zoukankan      html  css  js  c++  java
  • Java将字符串写入文件与将文件内容读取到字符串

    原文:http://blog.csdn.net/liuweiyuxiang/article/details/69487326

    将字符串写入文件

    方法一

    [java] view plain copy
     
    1. public void WriteStringToFile(String filePath) {  
    2.         try {  
    3.             File file = new File(filePath);  
    4.             PrintStream ps = new PrintStream(new FileOutputStream(file));  
    5.             ps.println("http://www.jb51.net");// 往文件里写入字符串  
    6.             ps.append("http://www.jb51.net");// 在已有的基础上添加字符串  
    7.         } catch (FileNotFoundException e) {  
    8.             // TODO Auto-generated catch block  
    9.             e.printStackTrace();  
    10.         }  
    11.     }  


    方法二

    [java] view plain copy
     
    1. public void WriteStringToFile2(String filePath) {  
    2.       try {  
    3.           FileWriter fw = new FileWriter(filePath, true);  
    4.           BufferedWriter bw = new BufferedWriter(fw);  
    5.           bw.append("在已有的基础上添加字符串");  
    6.           bw.write("abc  ");// 往已有的文件上添加字符串  
    7.           bw.write("def  ");  
    8.           bw.write("hijk ");  
    9.           bw.close();  
    10.           fw.close();  
    11.       } catch (Exception e) {  
    12.           // TODO Auto-generated catch block  
    13.           e.printStackTrace();  
    14.       }  
    15.   }  

    方法三

    [java] view plain copy
     
    1. public void WriteStringToFile3(String filePath) {  
    2.         try {  
    3.             PrintWriter pw = new PrintWriter(new FileWriter(filePath));  
    4.             pw.println("abc ");  
    5.             pw.println("def ");  
    6.             pw.println("hef ");  
    7.             pw.close();  
    8.         } catch (IOException e) {  
    9.             // TODO Auto-generated catch block  
    10.             e.printStackTrace();  
    11.         }  
    12.     }  

    方法四

    [java] view plain copy
     
    1. public void WriteStringToFile4(String filePath) {  
    2.         try {  
    3.             RandomAccessFile rf = new RandomAccessFile(filePath, "rw");  
    4.             rf.writeBytes("op ");  
    5.             rf.writeBytes("app ");  
    6.             rf.writeBytes("hijklllll");  
    7.             rf.close();  
    8.         } catch (IOException e) {  
    9.             e.printStackTrace();  
    10.         }  
    11.     }  

    方法五

    [java] view plain copy
     
    1. public void WriteStringToFile5(String filePath) {  
    2.         try {  
    3.             FileOutputStream fos = new FileOutputStream(filePath);  
    4.             String s = "http://www.jb51.netl";  
    5.             fos.write(s.getBytes());  
    6.             fos.close();  
    7.         } catch (Exception e) {  
    8.             // TODO Auto-generated catch block  
    9.             e.printStackTrace();  
    10.         }  
    11.     }  

    将文件内容读取到字符串
    方法一

    [java] view plain copy
     
    1. /** 
    2.  
    3. * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 
    4.  
    5. * 当然也是可以读字符串的。 
    6.  
    7. */  
    8.   
    9.     /* 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/  
    10.   
    11.     public String readString1()  
    12.   
    13.     {  
    14.   
    15.         try  
    16.   
    17.         {  
    18.   
    19.             //FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。   
    20.   
    21.             FileInputStream inStream=this.openFileInput(FILE_NAME);  
    22.   
    23.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    24.   
    25.             byte[] buffer=new byte[1024];  
    26.   
    27.             int length=-1;  
    28.   
    29.             while( (length = inStream.read(buffer) != -1)  
    30.   
    31.             {  
    32.   
    33.                 bos.write(buffer,0,length);  
    34.   
    35.                 // .write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream.  
    36.   
    37.                 //  当流关闭以后内容依然存在  
    38.   
    39.             }  
    40.   
    41.             bos.close();  
    42.   
    43.             inStream.close();  
    44.   
    45.             return bos.toString();     
    46.   
    47.             // 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么?  
    48.   
    49.             // return new String(bos.toByteArray(),"UTF-8");         
    50.   
    51.         }  
    52.   
    53.     }  

    方法二

    [java] view plain copy
     
    1. // 有人说了 FileReader  读字符串更好,那么就用FileReader吧  
    2.   
    3.     // 每次读一个是不是效率有点低了?  
    4.   
    5.     private static String readString2()  
    6.   
    7.     {  
    8.   
    9.         StringBuffer str=new StringBuffer("");  
    10.   
    11.         File file=new File(FILE_IN);  
    12.   
    13.         try {  
    14.   
    15.             FileReader fr=new FileReader(file);  
    16.   
    17.             int ch = 0;  
    18.   
    19.             while((ch = fr.read())!=-1 )  
    20.   
    21.             {  
    22.   
    23.                 System.out.print((char)ch+" ");   
    24.   
    25.             }  
    26.   
    27.             fr.close();  
    28.   
    29.         } catch (IOException e) {  
    30.   
    31.             // TODO Auto-generated catch block  
    32.   
    33.             e.printStackTrace();  
    34.   
    35.             System.out.println("File reader出错");  
    36.   
    37.         }  
    38.   
    39.         return str.toString();  
    40.   
    41.     }  

    方法三

    [java] view plain copy
     
    1. /*按字节读取字符串*/  
    2.   
    3. /* 个人感觉最好的方式,(一次读完)读字节就读字节吧,读完转码一次不就好了*/  
    4.   
    5. private static String readString3()  
    6.   
    7. {  
    8.   
    9.     String str="";  
    10.   
    11.     File file=new File(FILE_IN);  
    12.   
    13.     try {  
    14.   
    15.         FileInputStream in=new FileInputStream(file);  
    16.   
    17.         // size  为字串的长度 ,这里一次性读完  
    18.   
    19.         int size=in.available();  
    20.   
    21.         byte[] buffer=new byte[size];  
    22.   
    23.         in.read(buffer);  
    24.   
    25.         in.close();  
    26.   
    27.         str=new String(buffer,"GB2312");  
    28.   
    29.     } catch (IOException e) {  
    30.   
    31.         // TODO Auto-generated catch block  
    32.   
    33.         return null;  
    34.   
    35.         e.printStackTrace();  
    36.   
    37.     }  
    38.   
    39.     return str;  
    40.   
    41. }  

    方法四

    [java] view plain copy
     
      1. /*InputStreamReader+BufferedReader读取字符串  , InputStreamReader类是从字节流到字符流的桥梁*/  
      2.   
      3.     /* 按行读对于要处理的格式化数据是一种读取的好方式 */  
      4.   
      5.     private static String readString4()  
      6.   
      7.     {  
      8.   
      9.         int len=0;  
      10.   
      11.         StringBuffer str=new StringBuffer("");  
      12.   
      13.         File file=new File(FILE_IN);  
      14.   
      15.         try {  
      16.   
      17.             FileInputStream is=new FileInputStream(file);  
      18.   
      19.             InputStreamReader isr= new InputStreamReader(is);  
      20.   
      21.             BufferedReader in= new BufferedReader(isr);  
      22.   
      23.             String line=null;  
      24.   
      25.             while( (line=in.readLine())!=null )  
      26.   
      27.             {  
      28.   
      29.                 if(len != 0)  // 处理换行符的问题  
      30.   
      31.                 {  
      32.   
      33.                     str.append(" "+line);  
      34.   
      35.                 }  
      36.   
      37.                 else  
      38.   
      39.                 {  
      40.   
      41.                     str.append(line);  
      42.   
      43.                 }  
      44.   
      45.                 len++;  
      46.   
      47.             }  
      48.   
      49.             in.close();  
      50.   
      51.             is.close();  
      52.   
      53.         } catch (IOException e) {  
      54.   
      55.             // TODO Auto-generated catch block  
      56.   
      57.             e.printStackTrace();  
      58.   
      59.         }  
      60.   
      61.         return str.toString();  
      62.   
      63.     }  
  • 相关阅读:
    万能的everything彻底解决mysql问题
    乱码问题
    机器学习学习规划
    NG机器学习笔记
    书籍与博客
    技术规划
    反置数
    多个接口出现同名函数的处理-转
    接口-imploements-委托
    接口使用中,对象生命周期管理-转
  • 原文地址:https://www.cnblogs.com/shihaiming/p/8621604.html
Copyright © 2011-2022 走看看