将java字符串写入txt文件方法总结
方法一
1 2 3 4 5 6 7 |
try { BufferedWriter out = new BufferedWriter(new FileWriter("那啥快看.txt")); out.write("791202.com"); out.close(); System.out.println("文件创建成功!"); } catch (IOException e) { } |
方法二
1 2 3 4 5 6 7 8 9 10 11 |
File f=new File("C:\\那啥快看.txt"); FileWriter fw; try { fw=new FileWriter(f); String str="791202.com"; fw.write(str); fw.close(); } catch (IOException e) { e.printStackTrace(); } |
方法三
1 2 3 4 5 6 7 8 9 10 11 12 13 |
File f= new File("C:\\那啥快看.txt"); // 声明File对象 OutputStream out = null; // 准备好一个输出的对象 out = new FileOutputStream(f); // 通过对象多态性,进行实例化 String str = "791202.com"; // 准备一个字符串 byte b[] = str.getBytes(); // 只能输出byte数组,所以将字符串变为byte数组 out.write(b); // 将内容输出, out.close(); |
0