您的位置:首页 > 编程语言 > Java开发

JAVA对文件的读操作总结

2015-09-28 20:18 489 查看
1.以字节的方式读取文件(单次读取1字节,单次读取多个字节)

2.以字符的方式读取文件(单次读取1字符,单次读取多个字符)

3.以行的方式读取文件

4.随机的方式读取文件

5.在文件尾部添加数据(RandomAccessFile,FileWriter)

6.大文件的读取

package com.javaIo.readFile;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
* Package: com.javaIo.readFile
*
* File: ReadMethods.java
*
* Copyright @ 2015 Corpration Name
*
*/
public class ReadMethods {

/**
* 每次读取一个字节
*/
public static void ReadByOneByte(String fileName){
File file = new File(fileName);
try {
FileInputStream fis = new FileInputStream(file);
int size;
//每次读取输入流的一个字节
while((size = fis.read()) != -1){
System.out.write(size);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
};
}

/**
* 每次读取多个字节
*/
public static void ReadByBytes(String fileName){
File file = new File(fileName);
byte[] bt = new byte[100];//每次所要读取的字节数,每次读取后存入byte[],这里是100字节
try {
FileInputStream fis = new FileInputStream(file);
int size;
while((size = fis.read(bt)) != -1){
System.out.println(new String(bt, 0, size));
// System.out.write(bt, 0, size);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 每次读取一个字符
*/
public static void ReadByOneChar(String fileName){

File file = new File(fileName);
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
int size;
//每次读取一个字符
while((size = isr.read()) != -1){
System.out.write(size);

}
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 每次读取多个字符
*/
public static void ReadByChars(String fileName){

File file = new File(fileName);
char[] cha = new char[50];//每次读取50个字符,存入字符数组
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
int size;
//每次读取30个字符的数据
while((size = isr.read(cha)) != -1){
System.out.println(new String(cha, 0, size));
}
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 每次读取一行
*/
public static void ReadByLine(String fileName){

File file = new File(fileName);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String temp;
//读取的时候每次读取一行
while((temp = br.readLine()) != null){
System.out.println(temp);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 随机方式读取
*/
public static void ReadByRandom(String fileName){
File file = new File(fileName);
byte[] bt = new byte[100];//每次读取的100个字节
try {
//随机读取
RandomAccessFile ra = new RandomAccessFile(file, "r");
int size;
while((size = ra.read(bt)) != -1){
System.out.println(new String(bt, 0, size));
}

ra.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 在文件末尾添加数据,通过RandomAccessFile的方式
*
*/
public static void appendDataA(String fileName,String content){
File file = new File(fileName);
try {
RandomAccessFile ra = new RandomAccessFile(file, "rw");
long length = ra.length();
ra.seek(length);
ra.writeBytes(content);
ra.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 使用FileWriter的write方法
*
*/
public static void appendDataB(String fileName,String content){

File file = new File(fileName);
FileWriter fw = null;
try {
//构建一个写文件器,其中的true表示以追加的形式写入数据
fw = new FileWriter(file, true);
fw.write(content);

} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

/**
* 读取大文件,设置缓存
*/
public static void ReadBigFile(String fileName){

File file = new File(fileName);
byte[] bt = new byte[1024];
int bufferSize = 1024;
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);

try {
FileInputStream fis = new FileInputStream(file);
FileChannel chanel = fis.getChannel();
int size;
while((size = chanel.read(byteBuffer)) != -1){
byteBuffer.rewind();
byteBuffer.get(bt);
System.out.println(new String(bt, 0, size));
byteBuffer.clear();
}
chanel.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息