- wio
-
使用FileOutputStream流往一个文件里面写入数据
packagerestudy流;
importjava.io.*;
publicclass TestFileOutputStream {
publicstaticvoid main(String args[]) {
intb = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = newFileInputStream("D:/restudyJAVA/restudy3/TestInputStream.txt");
out = new FileOutputStream("D:/restudyJAVA/restudy3/TestFileOutputStream1.java");
// 指明要写入数据的文件,如果指定的路径中不存在TestFileOutputStream1.java这样的文件,则系统会自动创建一个
while ((b = in.read()()) != -1) {
out.write(b);
// 调用write(int c)方法把读取到的字符全部写入到指定文件中去
}
in.close();
out.close();
} catch (FileNotFoundException e) {
System.out.println("文件读取失败");
System.exit(-1);// 非正常退出
} catch (IOException e1) {
System.out.println("文件复制失败!");
System.exit(-1);
}
System.out
.println("TestFileInputStream.java文件里面的内容已经成功复制到文件TestFileOutStream1.java里面");
}
}
- cloud123
-
Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法。Writer中 write(),flush()和close()方法为抽象方法。子类应该分别实现他们。
Java IO已经为我们提供了三个方便的Reader的实现类,FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader, 它是字节转换为字符的桥梁。你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等。
FileReader读txt文件例子
FileReader fr = new FileReader("D:/Test.txt");
int ch = 0;
while((ch = fr.read())!=-1 ){
System.out.print( (char)ch );
}
其中read()方法返回的是读取得下个字符。
InputStreamReader读txt文件例子
InputStream is = new FileInputStream(new File("D:/Test.txt"));
InputStreamReader fr = new InputStreamReader(is);
int ch = 0;
while((ch = fr.read())!=-1 ){
System.out.print((char)ch);
}
这和FileReader并没有什么区别,事实上在FileReader中的方法都是从InputStreamReader中继承过来的。 read()方法是比较好费时间的,如果为了提高效率,我们可以使用BufferedReader对Reader进行包装,这样可以提高读取得速度,我们可以一行一行的读取文本,使用 readLine()方法。
BufferedReader br = new BufferedReader(new FileReader("Test.txt")));
String data = br.readLine();//一次读入一行,直到读入null为文件结束
while( data!=null){
System.out.println(data);
data = br.readLine(); //接着读下一行
}
当你明白了如何用Reader来读取文本文件的时候那么用Writer写文件同样非常简单。有一点需要注意,当你写文件的时候,为了提高效率,写入的数据会先放入缓冲区,然后写入文件。因此有时候你需要主动调用flush()方法。
有读就有写,写文本文件可以使用PrintWriter,FileWriter,BufferedWriter。
FileWriter fw = new FileWriter("D:/Test.txt");
String s = "hello world";
fw.write(s,0,s.length());
fw.flush();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:/Test1.txt"));
osw.write(s,0,s.length());
osw.flush();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("D:/Test2.txt")),true);
pw.println(s);
fw.close();
osw.close();
pw.close();
如果想接着写入某个文件。 声明时FileWriter fw = new FileWriter("log.txt",true);加个true就可以了。
File file = new File("D:/Test.txt");
File dest = new File("D:/new.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(dest));
String line = reader.readLine();
while(line!=null){
writer.write(line);
line = reader.readLine();
}
writer.flush();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
- 南yi
-
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!import java.io.*;
/** *//**
*
* 功能描述:创建
TXT
文件并进行读、写、修改操作
*
* @author <a href="mailto:zhanghhui@1*.com">KenZhang</a>
* @version 1.0
* Creation date: 2007-12-18 - 下午
06:48:45
*/
public class ReadWriteFile {
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "D:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";
/** *//**
* 创建文本文件
.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!
");
}
}
/** *//**
* 读取文本文件
.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "/r/n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件内容是
:"+ "/r/n" + readStr);
return readStr;
}
/** *//**
* 写文件
.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先读取原有文件内容,然后进行写入操作
String filein = newStr + "/r/n" + readStr + "/r/n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自动生成
catch
块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成
catch
块
e2.printStackTrace();
}
}
}
}
/** *//**
* 将文件中指定内容的第一行替换为其它内容
.
*
* @param oldStr
* 查找内容
* @param replaceStr
* 替换内容
*/
public static void replaceTxtByStr(String oldStr,String replaceStr) {
String temp = "";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
for (int j = 1; (temp = br.readLine()) != null
&& !temp.equals(oldStr); j++) {
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
// 将内容插入
buf = buf.append(replaceStr);
// 保存该行后面的内容
while ((temp = br.readLine()) != null) {
buf = buf.append(System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/** *//**
* main方法测试
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
ReadWriteFile.creatTxtFile();
ReadWriteFile.readTxtFile();
ReadWriteFile.writeTxtFile("20080808:12:13");
// ReadWriteFile.replaceTxtByStr("ken", "zhang");
}
}
- cloudcone
-
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(new File("a.txt")));
bufferedWriter = new BufferedWriter(new FileWriter(new File("b.txt")));
String lineStr = "";
while((lineStr = bufferedReader.readLine()) != null){
bufferedWriter.write(lineStr);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 再也不做稀饭了
-
用数据流读取a.txt文件中的数据
将数据解析排序
将数据写入到b.txt文件中
大概就这个思路 具体实现请自行bdidu!
- 苏萦
-
一行一行放入,一行一行读再用Double.parseDouble()方法解析