首页 > 编程 > Java > 正文

java读写文件

2019-11-09 20:06:21
字体:
来源:转载
供稿:网友
import java.io.*;class FileDemo { public static void main(String[] args) throws IOException{ String path = "d://demo//1.txt"; File f =new File(path); f.createNewFile(); if(f.exists()) System.out.PRintln("exists"); String str = "hello"; FileWriter fw = null; fw = new FileWriter(path, true); //if true, then bytes will be written to the end of the file rather than the beginning fw.write(str); fw.flush();//Flushes the stream. fw.close(); StringBuilder build = new StringBuilder(); FileReader reader = new FileReader(path); int ch; while((ch = reader.read())!= -1){ build.append((char)ch); } String str2 = build.toString(); System.out.println(str2); } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表