文件相关

我是一条分割线
追加文件内容
 import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AppendToFile {
    /**
     * A方法追加文件:使用RandomAccessFile
     */
    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();
        }
    }

    /**
     * B方法追加文件:使用FileWriter
     */
    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);
       /* //按方法B追加文件
        AppendToFile.appendMethodB(fileName, content);
        AppendToFile.appendMethodB(fileName, "append end. \n");
        //显示文件内容
        ReadFromFile.readFileByBytes(fileName);*/
       // ReadFromFile.readFileByLines(fileName);
    }
}


/**
      * 写入结果
      * @param s
      */
      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]);
  }




我是一条分割线



/**
      * 读取文件
      * @param path     文件路径
      * @param jsonContent   需要解析的内容
      * @return
      * @throws IOException
      */
      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 ;

     }

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

/**
      * 读取文件
      * @param path     文件路径
      * @param jsonContent   需要解析的内容
      * @return
      * @throws IOException
      */
      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 ) {
                        }
                   });
     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值