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

╰半夏微凉° 2022-05-16 12:13 363阅读 0赞

“日暮乡关何处是,烟波江上使人愁”
将字符串写入文件

方法一

  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. ps.close();
  8. } catch (FileNotFoundException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. }

方法二

  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. public String readString1()
  4. {
  5. try
  6. {
  7. //FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
  8. FileInputStream inStream=this.openFileInput(FILE_NAME);
  9. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  10. byte[] buffer=new byte[1024];
  11. int length=-1;
  12. while( (length = inStream.read(buffer) != -1)
  13. {
  14. bos.write(buffer,0,length);
  15. // .write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream.
  16. // 当流关闭以后内容依然存在
  17. }
  18. bos.close();
  19. inStream.close();
  20. return bos.toString();
  21. // 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么?
  22. // return new String(bos.toByteArray(),"UTF-8");
  23. }
  24. }

方法二

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

发表评论

表情:
评论列表 (有 0 条评论,363人围观)

还没有评论,来说两句吧...

相关阅读