java写入文件的几种方法小结 http://www.111cn.net/jsp/Java/57796.htm
Java中FileWriter写入文本换行 http://632977922.blog.51cto.com/6231950/1067874
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.anjoyo.test; import java.io.FileWriter; import java.io.IOException; public class TestFileWriter { public static void main(String[] args) throws IOException{ //举例需要,这里用throws //\r\n为换行符 FileWriter fw = new FileWriter("D:\\1.txt"); fw.write("第一行\r\n"); //或者获得系统换行符 String str = "第二行" + System.getProperty("line.separator"); fw.write(str); fw.write("第三行"); fw.close(); /* * windows下的文本文件换行符:\r\n linux/unix下的文本文件换行符:\r * Mac下的文本文件换行符:\n */ } } |
Java读取txt文件和写入txt文件 http://blog.csdn.net/monkeyduck/article/details/11525771
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package edu.thu.keyword.test; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; public class cin_txt { static void main(String args[]) { try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw /* 读入TXT文件 */ String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径 File filename = new File(pathname); // 要读取以上路径的input。txt文件 InputStreamReader reader = new InputStreamReader( new FileInputStream(filename)); // 建立一个输入流对象reader BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = ""; line = br.readLine(); while (line != null) { line = br.readLine(); // 一次读入一行数据 } /* 写入Txt文件 */ File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("我会写入文件啦\r\n"); // \r\n即为换行 out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 } catch (Exception e) { e.printStackTrace(); } } } |
java将字符串写入到txt文件中 http://zhangbaoming815.iteye.com/blog/1578438
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.RandomAccessFile; public class WriteStringToTxt { public void WriteStringToFile(String filePath) { try { File file = new File(filePath); PrintStream ps = new PrintStream(new FileOutputStream(file)); ps.println("http://www.docin.com/p-315288370.html");// 往文件里写入字符串 ps.append("http://www.docin.com/p-315288370.html");// 在已有的基础上添加字符串 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void WriteStringToFile2(String filePath) { try { FileWriter fw = new FileWriter(filePath, true); BufferedWriter bw = new BufferedWriter(fw); bw.append("在已有的基础上添加字符串"); bw.write("abc\r\n ");// 往已有的文件上添加字符串 bw.write("def\r\n "); bw.write("hijk "); bw.close(); fw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void WriteStringToFile3(String filePath) { try { PrintWriter pw = new PrintWriter(new FileWriter(filePath)); pw.println("abc "); pw.println("def "); pw.println("hef "); pw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void WriteStringToFile4(String filePath) { try { RandomAccessFile rf = new RandomAccessFile(filePath, "rw"); rf.writeBytes("op\r\n"); rf.writeBytes("app\r\n"); rf.writeBytes("hijklllll"); rf.close(); } catch (IOException e) { e.printStackTrace(); } } public void WriteStringToFile5(String filePath) { try { FileOutputStream fos = new FileOutputStream(filePath); String s = "http://www.docin.com/p-315288370.html"; fos.write(s.getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { String filePath = "E:\\link.txt"; // new WriteStringToTxt().WriteStringToFile(filePath); // new WriteStringToTxt().WriteStringToFile2(filePath); // new WriteStringToTxt().WriteStringToFile3(filePath); // new WriteStringToTxt().WriteStringToFile4(filePath); new WriteStringToTxt().WriteStringToFile5(filePath); } } |
java追加内容到文件末尾 http://www.mekau.com/383.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.RandomAccessFile; /** * 描述:追加内容到文件末尾 * @author Administrator * */ public class WriteStreamAppend { /** * 追加文件:使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true * * @param fileName * @param content */ public static void method1(String file, String conent) { BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); out.write(conent); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 追加文件:使用FileWriter * * @param fileName * @param content */ public static void method2(String fileName, String content) { try { // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 追加文件:使用RandomAccessFile * * @param fileName * 文件名 * @param content * 追加的内容 */ public static void method3(String fileName, String content) { try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); // 将写文件指针移到文件尾。 randomFile.seek(fileLength); String s2=new String(content.getBytes("GBK"),"iso8859-1"); randomFile.writeBytes(s2); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("start"); method1("c:/work/test.txt", "追加到文件的末尾1"); method2("c:/work/test.txt", "追加到文件的末尾2"); method3("c:/work/test.txt", "追加到文件的末尾3"); System.out.println("end"); } } |
Java读写文件的几种方法 http://www.cnblogs.com/qianbi/p/3378466.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class MainClass { public static void main(String[] args) { } /** * 使用FileWriter类写文本文件 */ public static void writeMethod1() { String fileName="C:\\kuka.txt"; try { //使用这个构造函数时,如果存在kuka.txt文件, //则先把这个文件给删除掉,然后创建新的kuka.txt FileWriter writer=new FileWriter(fileName); writer.write("Hello Kuka:\n"); writer.write(" My name is coolszy!\n"); writer.write(" I like you and miss you。"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用FileWriter类往文本文件中追加信息 */ public static void writeMethod2() { String fileName="C:\\kuka.txt"; try { //使用这个构造函数时,如果存在kuka.txt文件, //则直接往kuka.txt中追加字符串 FileWriter writer=new FileWriter(fileName,true); SimpleDateFormat format=new SimpleDateFormat(); String time=format.format(new Date()); writer.write("\n\t"+time); writer.close(); } catch (IOException e) { e.printStackTrace(); } } //注意:上面的例子由于写入的文本很少,使用FileWrite类就可以了。但如果需要写入的 //内容很多,就应该使用更为高效的缓冲器流类BufferedWriter。 /** * 使用BufferedWriter类写文本文件 */ public static void writeMethod3() { String fileName="C:/kuka.txt"; try { BufferedWriter out=new BufferedWriter(new FileWriter(fileName)); out.write("Hello Kuka:"); out.newLine(); //注意\n不一定在各种计算机上都能产生换行的效果 out.write(" My name is coolszy!\n"); out.write(" I like you and miss you。"); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 使用FileReader类读文本文件 */ public static void readMethod1() { String fileName="C:/kuka.txt"; int c=0; try { FileReader reader=new FileReader(fileName); c=reader.read(); while(c!=-1) { System.out.print((char)c); c=reader.read(); } reader.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 使用BufferedReader类读文本文件 */ public static void readMethod2() { String fileName="c:/kuka.txt"; String line=""; try { BufferedReader in=new BufferedReader(new FileReader(fileName)); line=in.readLine(); while (line!=null) { System.out.println(line); line=in.readLine(); } in.close(); } catch (IOException e) { e.printStackTrace(); } } } |