免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2140 | 回复: 0
打印 上一主题 下一主题

java写入文本文件代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 03:29 |只看该作者 |倒序浏览
java写入文本文件代码
From:http://www.homeandlearn.co.uk/java/write_to_textfile.html

  1. package net.chinaunix.blog.hzm.text;

  2. import java.io.FileWriter;
  3. import java.io.PrintWriter;
  4. import java.io.IOException;

  5. public class WriteFile {

  6.     private String path;
  7.     private boolean appendToFile = false;
  8.     
  9.     public WriteFile(String filePath){
  10.         path = filePath;
  11.     }
  12.     
  13.     public WriteFile(String filePath, boolean appendvalue){
  14.         path = filePath;
  15.         appendToFile = appendvalue;
  16.         
  17.     }
  18.     
  19.     public void writeToFile(String textLine) throws IOException{
  20.         
  21.         FileWriter writer = new FileWriter(path,appendToFile);
  22.         PrintWriter printer = new PrintWriter(writer);
  23.         printer.printf("%s"+"%n",textLine);
  24.         printer.close();
  25.         /*The %s between double quotes means a string of characters of any length.
  26.           The %n means a newline. So we're telling the printf method to format a      string  of characters and add a newline at the end.
  27.          */
  28.         
  29.     }
  30.     
  31. }

  1. package net.chinaunix.blog.hzm.text;

  2. import java.io.IOException;

  3. public class FileData {

  4.     public static void main(String[] args) throws IOException{
  5.         
  6.         String filePath = "C:/text.txt";
  7.         
  8.     
  9.         try{
  10.             WriteFile writer = new WriteFile(filePath,true);
  11.           &n bsp; writer.writeToFile("This is another line..");
  12.         }catch(IOException e){
  13.             System.out.println(e.getMessage());
  14.         }
  15.     }
  16. }
 java.io.FileWriter

Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

java.io.PrintWriter

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP