淘先锋技术网

首页 1 2 3 4 5 6 7
我是一条分割线
追加文件内容
 import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AppendToFile {
   
    public static void appendMethodA(String fileName, String content) {
        try {
            // 打开一个随机访问文件流,按读写方式
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            // 文件长度,字节数
            long fileLength = randomFile.length();
            //将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
            randomFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   
    public static void appendMethodB(String fileName, String content) {
        try {
            //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String fileName = "E:/newTemp.dat";
        String content = "new append!";
        //按方法A追加文件
        AppendToFile.appendMethodA(fileName, content);
        AppendToFile.appendMethodA(fileName, "append end.");
        //显示文件内容
        ReadFromFile.readFileByBytes(fileName);//.readFileByLines(fileName);
      
       // ReadFromFile.readFileByLines(fileName);
    }
}


      public void putRun(String s ) {
          String runPath = "mnt/sdcard/wlk/log/log.txt" ;
           try {
              FileWriter fw = null ;
          // true为在文本末尾追加
               fw = new FileWriter( runPath , true );
               fw .write( s );
               fw .flush();
               fw .close();
          } catch (Exception e ) {           
          }
     }

我是一条分割线
向文件中添加内容
String str=“SB”;

FileWriter fw=new FileWriter("D:\\log.txt");
fw.write(str);
fw.flush();
fw.close();

我是一条分割线

创建文件
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "");
(SD卡目录,文件分割符,文件名) 声明一个文件
if(file.exists){//判断文件是否存在
file.delete;//存在就删除
file.createNewFile//创建文件
}

ouyputStream ot = new FileOutputStream(file,true)//文件输出流
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"><uses-permission>


break  跳出一层循环
加注释跳出注释那层
in:
for (){
for(){
 break in;
  }
}
我是一条分割线
刷新媒体库
  //filename是我们的文件全名,包括后缀
         private void updateGallery(String filename ){
              MediaScannerConnection.scanFile( this ,
               new String[] { filename }, null ,
               new MediaScannerConnection.OnScanCompletedListener() {
               public void onScanCompleted(String path , Uri uri ) {
               Log.i( "ExternalStorage" , "Scanned " + path + ":" );
               Log.i( "ExternalStorage" , "-> uri=" + uri );
               }
               });
          }


      //filename是我们的文件全名,包括后缀哦(在广播中使用)
    private void updateGallery(String filename ){
     
          MediaScannerConnection.scanFile( c .getApplicationContext(),
           new String[] { filename }, null ,
           new MediaScannerConnection.OnScanCompletedListener() {
           public void onScanCompleted(String path , Uri uri ) {
           }
           });
     }



我是一条分割线

创建删除写入文件权限   
     <!--往sdcard中写入数据的权限 -->
     < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
     <!--在sdcard中创建/删除文件的权限 -->
     < uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    我是一条你看不见的分割线

读取文件信息并一行一行的显示
File file=new File("/mnt/sdcard/aa.txt");
  FileReader fr=new FileReader(file);
  BufferedReader br=new BufferedReader(fr);
  String temp=null;
  String s="";
  while((temp=br.readLine())!=null){
   s+=temp+"\n";
  }
  String [] ss=s.split("\n");
  for (int i = 0; i < ss.length; i++) {
   System.out.println(ss[i]);
  }




我是一条分割线



      public StringFileOut(String path ) throws IOException {
          String content = "" ; // 文件内容字符串
           // 打开文件
          File file = new File( path );
           // 如果path是传递过来的参数,可以做一个非目录的判断
           if ( file .isDirectory()) {
          } else {
              BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ));
               if ( in != null ) {
                   String line ;
                    // 分行读取
                    while (( line = in .readLine()) != null ) {
                         content += line ;

                    // coordinate = line .split( "\\," );   分行读取到逗号后的数据
                   }
                    in .close();
              }
          }
           return content ;

     }

我是一条你看不见的分割线

      public String FileOut(String path , String jsonContent ) throws IOException {
          String content = "" ; // 文件内容字符串
           // 打开文件
          File file = new File( path );
           // 如果path是传递过来的参数,可以做一个非目录的判断
           if ( file .isDirectory()) {
          } else {
              BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ));
               if ( in != null ) {
                   String line ;
                    // 分行读取
                    while (( line = in .readLine()) != null ) {
                         content += line ;
                   }
                    in .close();
              }
          }
           return content ;

     }

我是一条你看不见的分割线

刷新媒体库
      // filename是我们的文件全名,包括后缀哦
      private void updateGallery(String filename ) {

          MediaScannerConnection.scanFile( c , new String[] { filename }, null ,
                    new MediaScannerConnection.OnScanCompletedListener() {
                         public void onScanCompleted(String path , Uri uri ) {
                        }
                   });
     }