概述
Java能实现文本文件输入输出的类种类繁多,正确使用的难度极大。本文旨在总结各种IO的实现方法。以下六个函数分别完整实现了六种IO,希望对大家有所帮助。
package apis;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Readfile {
/**
a method of readfile.
/
public static void main(String[] args) {
apis.Readfile.writer(“writer.txt”, “this is a n test of n writer”);
System.out.println(apis.Readfile.reader(“writer.txt”));
apis.Readfile.BufferedWriter(“bufferedwriter.txt”, “this is a n test of n bufferedwriter”);
System.out.println(apis.Readfile.reader(“bufferedwriter.txt”));
apis.Readfile.nioWriter(“niowriter.txt”, “this is a n test of n niowriter”);
System.out.println(apis.Readfile.nioReader(“niowriter.txt”));
}
/*
a method of readfile.
/
public static String reader(String path) {
try {
FileReader fr = new FileReader(path);
StringBuilder a = new StringBuilder();
while (true) {
int temp = fr.read();
if (temp == -1) {
break;
} else {
a.append((char) temp);
}
}
fr.close();
return a.toString();
} catch (Exception e) {
System.out.println(“reader error”);
}
return “”;
}
/*
a method of readfile.
/
public static void writer(String path, String info) {
try {
File file = new File(path);
// 创建文件
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// 向文件写入内容
writer.write(info);
writer.flush();
writer.close();
} catch (Exception e) {
System.out.println(“writer error”);
}
}
/*
a method of readfile.
/
public static String BufferedReader(String filename) {
StringBuilder a = new StringBuilder();
File file = new File(filename);
BufferedReader reader = null;
try {
// System.out.println(“以行为单位读取文件内容,一次读一整行:”);
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
a.append(tempString + ‘n’);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
//ignore
}
}
}
return a.toString();
}
/*
a method of readfile.
/
public static void BufferedWriter(String path, String info) {
try {
File writeName = new File(path);
writeName.createNewFile();
FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer);
out.write(info);
out.flush();
out.close();
} catch (IOException e) {
System.out.println(“Write error.”);
}
}
/*
a method of readfile.
*/
public static String nioReader(String path) {
FileInputStream fin;
try {
fin = new FileInputStream(path);
// 获取通道
FileChannel fc = fin.getChannel();
// 创建缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 读取数据到缓冲区
fc.read(buffer);
buffer.flip();
StringBuffer s = new StringBuffer();
while (buffer.remaining() > 0) {
byte b = buffer.get();
s.append((char) b);
// System.out.print(((char) b));
}
fin.close();
return s.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
System.out.println(“nio read error”);
}
return “”;
}
/**
a method of readfile.
*/
public static void nioWriter(String path, String info) {
try {
File file = new File(path);
FileOutputStream outputStream = new FileOutputStream(file);
FileChannel channel = outputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
String string = info;
buffer.put(string.getBytes());
buffer.flip(); // 此处必须要调用buffer的flip方法
channel.write(buffer);
channel.close();
outputStream.close();
} catch (Exception e) {
System.out.println(“nio write error”);
}
}
}
最后
以上就是甜甜洋葱为你收集整理的JAVA文本输入输出的全部内容,希望文章能够帮你解决JAVA文本输入输出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复