java eclipse 读取文件_怎样用eclipse读写文件

java eclipse 读取文件_怎样用eclipse读写文件展开全部要实现读写文件,可以使用到e69da5e6ba9062616964757a686964616f31333337613162java中的I/O流。具体代码如下:importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundExceptio…

大家好,欢迎来到IT知识分享网。java eclipse 读取文件_怎样用eclipse读写文件

展开全部

要实现读写文件, 可以使用到e69da5e6ba9062616964757a686964616f31333337613162java中的I/O流。具体代码如下:import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

/**

* java读写文件

* 读取d:/1.txt文件内容,写入f:/text.txt文件中.

*

* 写入文件换行用fw.write(“\r\n”);

* 或者fw.write(“\n”);

* @author young

*

*/

public class FileWriterTest {

// 读写文件

public static void rwFile(){

FileWriter fw = null;

BufferedReader br = null;

try {

fw = new FileWriter(“f:\\text.txt”, true);

br = new BufferedReader(new InputStreamReader(

new FileInputStream(“d:\\1.txt”), “UTF-8”));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(“文件内容: ” + line);

fw.write(line);

fw.flush();

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fw != null) {

try {

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

rwFile();

}

}

在D盘新建文本文件1.txt,输入任意内容,在eclipse中执行java代码,会在F盘生成text.txt文本文件,并把1.txt内容写入到text.txt中。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22309.html

(0)
上一篇 2024-01-07 17:15
下一篇 2024-01-07 18:15

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信