IO流的具体案例
来源:互联网 发布:数据透视表显示合计 编辑:程序博客网 时间:2024/06/05 18:47
public class IOUtils {
/**
* 以字符流,按行读取的方式,读取一个文件并输出
*
* BufferedReader
*
* FileReader
*
* FileInputStream
*/
public static void readFileByBufferedReaderLine(File file){
//目的:读取本地文件中的数据,打印到控制台显示
/*
* 1、从本地文件中读取数据到内存中:需要用到输入流,但是此案例,要求以字符为单位读取数据内容
* 所以用到:字符输入流
* 2、将读取到内存中的数据,通过控制台打印出来,涉及到输出流。
*/
//声明一个BufferedReader类型变量
BufferedReader reader = null;
String tempString = null;
try {
//实例化该变量,需要传入参数Reader类型的对象
reader = new BufferedReader(new FileReader(file));
//使用Reader来读取文件内容,按行读取,一次读取一行
while((tempString = reader.readLine()) != null){
System.out.println(tempString);
}
reader.close();
//FileNotFoundException 捕获的是new FileReader(file)这里面的异常
} catch (FileNotFoundException e) {
e.printStackTrace();
//IOException 捕获的是 reader.readLine()这个方法里的异常
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字符流以字符为单位读取文件
* InputStreamReader
*
* FileInputStream
*/
public static void readFileByStreamReader(File file){
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempChar;
while((tempChar = reader.read()) != -1){
System.out.print((char)tempChar);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字节流,以字节为单位,读取文件
*
* FileInputStream
*/
public static void readFileByFileInputStream(File file){
InputStream inputStream = null;
byte[] tempByte = new byte[1024];
try {
int byteRead;
inputStream = new FileInputStream(file);
while((byteRead = inputStream.read(tempByte)) != -1){
String tempString = new String(tempByte, 0, tempByte.length);
System.out.println(tempString);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 以文件字节流写入文件
*
* FileOutputStream
*/
public static void writeByFileOutputStream(File file, String content){
FileOutputStream fileOutputStream = null;
try {
//如下构造方法FileOutputStream如果第二个参数省略,则默认为false,即:不追加写入。直接覆盖原文件中的内容
fileOutputStream = new FileOutputStream(file, true);
byte[] contents = content.getBytes();
fileOutputStream.write(contents);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 以字符流的方式写入文件
*/
public static void writeByBufferedWriter(File file, String content){
BufferedWriter bufferedWriter = null;
try {
//如下构造方法FileWriter如果第二个参数省略,则默认为false,即:不追加写入。
// 直接覆盖原文件中的内容
bufferedWriter = new BufferedWriter(new FileWriter(file, true));
// char[] contents = content.toCharArray();
// bufferedWriter.write(contents);
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件的复制
* 1、涉及到输入流
* 2、输出流
*/
public static void copyFile(File oldFile, File newFile){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] readBytes = new byte[1024];
long length = 0;
int curLength;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
while((curLength = fileInputStream.read(readBytes)) != -1){
fileOutputStream.write(readBytes, 0, curLength);
length += curLength;
}
System.out.println("文件大小:" + length);
fileOutputStream.flush();
fileInputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 以字符流,按行读取的方式,读取一个文件并输出
*
* BufferedReader
*
* FileReader
*
* FileInputStream
*/
public static void readFileByBufferedReaderLine(File file){
//目的:读取本地文件中的数据,打印到控制台显示
/*
* 1、从本地文件中读取数据到内存中:需要用到输入流,但是此案例,要求以字符为单位读取数据内容
* 所以用到:字符输入流
* 2、将读取到内存中的数据,通过控制台打印出来,涉及到输出流。
*/
//声明一个BufferedReader类型变量
BufferedReader reader = null;
String tempString = null;
try {
//实例化该变量,需要传入参数Reader类型的对象
reader = new BufferedReader(new FileReader(file));
//使用Reader来读取文件内容,按行读取,一次读取一行
while((tempString = reader.readLine()) != null){
System.out.println(tempString);
}
reader.close();
//FileNotFoundException 捕获的是new FileReader(file)这里面的异常
} catch (FileNotFoundException e) {
e.printStackTrace();
//IOException 捕获的是 reader.readLine()这个方法里的异常
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字符流以字符为单位读取文件
* InputStreamReader
*
* FileInputStream
*/
public static void readFileByStreamReader(File file){
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempChar;
while((tempChar = reader.read()) != -1){
System.out.print((char)tempChar);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字节流,以字节为单位,读取文件
*
* FileInputStream
*/
public static void readFileByFileInputStream(File file){
InputStream inputStream = null;
byte[] tempByte = new byte[1024];
try {
int byteRead;
inputStream = new FileInputStream(file);
while((byteRead = inputStream.read(tempByte)) != -1){
String tempString = new String(tempByte, 0, tempByte.length);
System.out.println(tempString);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 以文件字节流写入文件
*
* FileOutputStream
*/
public static void writeByFileOutputStream(File file, String content){
FileOutputStream fileOutputStream = null;
try {
//如下构造方法FileOutputStream如果第二个参数省略,则默认为false,即:不追加写入。直接覆盖原文件中的内容
fileOutputStream = new FileOutputStream(file, true);
byte[] contents = content.getBytes();
fileOutputStream.write(contents);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 以字符流的方式写入文件
*/
public static void writeByBufferedWriter(File file, String content){
BufferedWriter bufferedWriter = null;
try {
//如下构造方法FileWriter如果第二个参数省略,则默认为false,即:不追加写入。
// 直接覆盖原文件中的内容
bufferedWriter = new BufferedWriter(new FileWriter(file, true));
// char[] contents = content.toCharArray();
// bufferedWriter.write(contents);
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件的复制
* 1、涉及到输入流
* 2、输出流
*/
public static void copyFile(File oldFile, File newFile){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] readBytes = new byte[1024];
long length = 0;
int curLength;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
while((curLength = fileInputStream.read(readBytes)) != -1){
fileOutputStream.write(readBytes, 0, curLength);
length += curLength;
}
System.out.println("文件大小:" + length);
fileOutputStream.flush();
fileInputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
- IO流的具体案例
- DBUtils的具体使用案例。
- java案例-基于IO流的登录注册案例
- IO流的分类及常用案例
- IO流_LineNumberReader的使用案例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- Java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- java IO流文件的读写具体实例
- 解析XML文件的具体案例
- 深度学习---反向传播的具体案例
- IO流_递归删除带内容的目录案例
- IO流_复制文本文件的5种方式案例
- 沈阳网赛1003 HDU 5894 hannnnah_j’s Biological Test
- Java System.arraycopy方法的使用
- JavaEE之初学者
- python3基础
- 码云上创建项目并clone到idea
- IO流的具体案例
- Caffe常见问题解决:NuGet Error,无法打开输入文件“opencv_calib3d240d.lib”
- 【数据库】封锁技术
- linux c捕获信号
- 手势识别
- android文本
- 静态成员的应用
- CodeForces 626B Cards(数学思维)
- Python学习资料总结