如何向一个txt文件尾添加字符串? 如题。如何在已有文件的文件尾的下一行添加一行字符串 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有一个想法不知道是否可行(因为没有试过),首先把所有的内容读到内存中(Buffer..string),然后在这个bs里边加字符,最后再写到文件中去. FileWriterpublic FileWriter(File file, boolean append) throws IOException在给出 File 对象的情况下构造一个 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。 参数:file - 要写入数据的 File 对象append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 抛出: IOException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它从以下版本开始: 1.4 package test;import java.io.*;public class Append { /** * @param args * @throws IOException * @throws IOException */ public static void main(String[] args) throws IOException { InputStreamReader isr = null; BufferedReader br = null; OutputStreamWriter osw = null; BufferedWriter pw = null; String line = null; int count = 0; try { isr = new FileReader("H:\\sn.txt"); br = new BufferedReader(isr); } catch (FileNotFoundException e) { e.printStackTrace(); } try { osw = new FileWriter("H:\\sn.txt",true); pw = new BufferedWriter(osw); pw.write("str"); pw.flush(); pw.newLine(); } catch (IOException e1) { e1.printStackTrace(); }finally{ pw.close(); osw.close(); br.close(); isr.close(); } }} 根据楼主说的改了该public class Append { /** * @param args * @throws IOException * @throws IOException */ public static void main(String[] args) throws IOException { OutputStreamWriter osw = null; BufferedWriter pw = null; try { osw = new FileWriter("H:\\sn.txt",true);//this file is already exist. pw = new BufferedWriter(osw); pw.write("str");//append this string "str" to the file sn.txt. pw.flush(); pw.newLine(); } catch (IOException e1) { e1.printStackTrace(); }finally{ pw.close(); osw.close(); } }} 嗬嗬,又是你呀!字符串前面?不是后面吗?假如你给windows的文件加的话,用“\r\n”比较好。符合windows风格。赫赫。 AWUSOFT的读入内存的想法,近似疯狂。这样对服务器来说,简直就是一种折磨。文件大一些,连续操作的话,用不了多久,就OutOfMemory了。 BufferedWriter类,支持newline方法的。也可以做到的. FileWriter f=new FileWriter(fileName,append);fileName - 一个字符串,表示与系统有关的文件名。append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。 InputStreamReader isr=new InputStreamReader("xx.txt");BufferedReader br=new BufferedReader(isr);OutputStreamReader osr=new OutputStreamReader(br);for(int i=0;i<br.readln();i++){ br.write(i);} 上面的写错了哈,不好意思 File f=new File("xx.txt"); FileOutputStream fos=new FileOutputStream(f);OutputStreamReader osr=new OutputStreamReader(br); String s="hello world";for(int i=0;i <s.length;i++){ osr.write(i); }osr.flush();osr.close();fos.close(); http://hi.baidu.com/johnsoncr/blog/item/31bbe73882bdf92097ddd8ce.html这里面介绍了三种方法 关于接口中的成员变量 请问:一般程序怎样高效地管理数据?使用数据库管理的前提是否是该机已经安装了数据库软件? 请问如何用java做一个简单的家庭日常记账软件? 请教java中的单例模型 关于 Swing 中下拉菜单类和复选框??????????????在线等 希望大家给我解释一下这个问题 用stringtokenizer分解一个字符串? java中如何实现文件拷贝? delphi vs java 请问关于java安装其他包后打包的问题 怎样将Unicode编码变化为对应字符 jdom中调用setAttribute(Sring key,Sring value)时,如果key中带有冒号,则报错
public FileWriter(File file,
boolean append)
throws IOException在给出 File 对象的情况下构造一个 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。 参数:
file - 要写入数据的 File 对象
append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
抛出:
IOException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
从以下版本开始:
1.4
import java.io.*;public class Append { /**
* @param args
* @throws IOException
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStreamReader isr = null;
BufferedReader br = null;
OutputStreamWriter osw = null;
BufferedWriter pw = null;
String line = null;
int count = 0;
try {
isr = new FileReader("H:\\sn.txt");
br = new BufferedReader(isr);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
osw = new FileWriter("H:\\sn.txt",true);
pw = new BufferedWriter(osw);
pw.write("str");
pw.flush();
pw.newLine();
} catch (IOException e1) {
e1.printStackTrace();
}finally{
pw.close();
osw.close();
br.close();
isr.close();
}
}
}
public class Append { /**
* @param args
* @throws IOException
* @throws IOException
*/
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = null;
BufferedWriter pw = null;
try {
osw = new FileWriter("H:\\sn.txt",true);//this file is already exist.
pw = new BufferedWriter(osw);
pw.write("str");//append this string "str" to the file sn.txt.
pw.flush();
pw.newLine();
} catch (IOException e1) {
e1.printStackTrace();
}finally{
pw.close();
osw.close();
}
}
}
不是后面吗?
假如你给windows的文件加的话,用“\r\n”比较好。符合windows风格。赫赫。
也可以做到的.
fileName - 一个字符串,表示与系统有关的文件名。
append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。
BufferedReader br=new BufferedReader(isr);
OutputStreamReader osr=new OutputStreamReader(br);
for(int i=0;i<br.readln();i++){
br.write(i);
}
File f=new File("xx.txt");
FileOutputStream fos=new FileOutputStream(f);
OutputStreamReader osr=new OutputStreamReader(br);
String s="hello world";
for(int i=0;i <s.length;i++){
osr.write(i);
}
osr.flush();
osr.close();
fos.close();
这里面介绍了三种方法