java怎么用流读取一个文件的内容然后添加到别的文件中?

Python019

java怎么用流读取一个文件的内容然后添加到别的文件中?,第1张

FileInputStream fis = new FileInputStream("d:/a.txt")//从a.txt中读出\x0d\x0aFileOutputStream fos = new FileOutputStream("d:/b.txt")//写到b.txt中去\x0d\x0aBufferedReader reader = new BufferedReader(new InputStreamReader(fis))\x0d\x0aBufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos))\x0d\x0aString temp\x0d\x0awhile((temp = reader.readLine())!= null){//一次读一行\x0d\x0awrite.write(temp)\x0d\x0a}\x0d\x0areader.close()\x0d\x0awrite.close()

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()

}

}

}