JAVA文本的输入输出

查阅资料整理后,总结如下。以下六个函数分别完整实现了六种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”);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值