java如何对文件追加写入
publicstaticvoidmain(String[]args)throwsException{try{BufferedReaderbr=newBufferedRea...
public static void main(String[] args)throws Exception{
try{
BufferedReader br = new BufferedReader(new FileReader("E:\\a.txt"));
String s;
while((s=br.readLine())!=null)
{
File f = new File("E:\\"+File.separator+"b.txt");
OutputStream out = new FileOutputStream(f,true);
out.write(s.getBytes());
out.close();
}
System.out.println("成功存入~!");
}catch(Exception e){
System.out.println("error:"+e);
}
}
}
这是我的代码,追加写入就乱码了... 展开
try{
BufferedReader br = new BufferedReader(new FileReader("E:\\a.txt"));
String s;
while((s=br.readLine())!=null)
{
File f = new File("E:\\"+File.separator+"b.txt");
OutputStream out = new FileOutputStream(f,true);
out.write(s.getBytes());
out.close();
}
System.out.println("成功存入~!");
}catch(Exception e){
System.out.println("error:"+e);
}
}
}
这是我的代码,追加写入就乱码了... 展开
5个回答
展开全部
java文件追加内容的三种方法:
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file,"rw");
long len = randomAccessFile.length();
randomAccessFile.seek(len);
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二:
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
bufferedOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}finally{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
展开全部
带编码处理
public static void main(String args[]) {
try {
InputStreamReader is = new InputStreamReader(new FileInputStream(
"E:\\a.txt"), "gbk");
BufferedReader br = new BufferedReader(is);
File f = new File("E:\\b.txt");
String s;
while ((s = br.readLine()) != null) {
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
bw.write(s);
bw.newLine();
bw.close();
}
System.out.println("成功存入~!");
} catch (Exception e) {
System.out.println("error:" + e);
}
}
public static void main(String args[]) {
try {
InputStreamReader is = new InputStreamReader(new FileInputStream(
"E:\\a.txt"), "gbk");
BufferedReader br = new BufferedReader(is);
File f = new File("E:\\b.txt");
String s;
while ((s = br.readLine()) != null) {
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
bw.write(s);
bw.newLine();
bw.close();
}
System.out.println("成功存入~!");
} catch (Exception e) {
System.out.println("error:" + e);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
读取文件内容使用的这些类:BufferedReader br = new BufferedReader(new FileReader("E:\\a.txt"));
然后写入的时候用的这个:OutputStream out = new FileOutputStream(f,true);
一个是字符流,一个是字节流。你试试把FileOutputStream改为FileWriter试试,或者怎么处理下用上BufferedWriter 和 FileWriter
然后写入的时候用的这个:OutputStream out = new FileOutputStream(f,true);
一个是字符流,一个是字节流。你试试把FileOutputStream改为FileWriter试试,或者怎么处理下用上BufferedWriter 和 FileWriter
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个不就是可以吗??
追问
不行啊,一追加就乱码了....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询