Java将字符串写入文件与将文件内容读取到字符串

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

将字符串写入文件

方法一

  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. }

方法二

  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\r\n ");// 往已有的文件上添加字符串
  7. bw.write("def\r\n ");
  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. }

方法三

  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. }

方法四

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

方法五

  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. }

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

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

方法二

  1. // 有人说了 FileReader  读字符串更好,那么就用FileReader吧
  2. // 每次读一个是不是效率有点低了?
  3. private static String readString2()
  4. {
  5. StringBuffer str=new StringBuffer("");
  6. File file=new File(FILE_IN);
  7. try {
  8. FileReader fr=new FileReader(file);
  9. int ch = 0;
  10. while((ch = fr.read())!=-1 )
  11. {
  12. System.out.print((char)ch+" ");
  13. }
  14. fr.close();
  15. } catch (IOException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. System.out.println("File reader出错");
  19. }
  20. return str.toString();
  21. }

方法三

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

方法四

    1. /*InputStreamReader+BufferedReader读取字符串  , InputStreamReader类是从字节流到字符流的桥梁*/
    2. /* 按行读对于要处理的格式化数据是一种读取的好方式 */
    3. private static String readString4()
    4. {
    5. int len=0;
    6. StringBuffer str=new StringBuffer("");
    7. File file=new File(FILE_IN);
    8. try {
    9. FileInputStream is=new FileInputStream(file);
    10. InputStreamReader isr= new InputStreamReader(is);
    11. BufferedReader in= new BufferedReader(isr);
    12. String line=null;
    13. while( (line=in.readLine())!=null )
    14. {
    15. if(len != 0)  // 处理换行符的问题
    16. {
    17. str.append("\r\n"+line);
    18. }
    19. else
    20. {
    21. str.append(line);
    22. }
    23. len++;
    24. }
    25. in.close();
    26. is.close();
    27. } catch (IOException e) {
    28. // TODO Auto-generated catch block
    29. e.printStackTrace();
    30. }
    31. return str.toString();
    32. }
上一篇:30分钟了解pandas


下一篇:POJ 1426 Find The Multiple(寻找倍数)