Java基础 4000 之基本IO操作
2017-07-11 21:18
459 查看
文章的内容参考了 http://blog.csdn.net/yczz/article/details/38761237,对博主表示感谢。
java的IO体系包括字符流和字节流,如下图所示:
下面对常见的IO操作举例,如果想要参考IO的其他流操纵,请阅读官方的API文档
import java.io.*;
import static java.lang.System.*;
/**
* Created by leo on 17-7-11.
*/
public class IONote {
public static void main(String[] args){
IONote note = new IONote();
// note.createFile("/home/leo/Java/io.txt");
// note.copyFile();
// note.countLines();
note.useBufferWriter();
}
public void createFile(String fileName){
File file = new File(fileName);
try {
file.createNewFile();
// out.println(file.getTotalSpace()/1024/1024/1024 + "G");
file.mkdirs();
out.println(file.getName());
out.println(file.getParent());
} catch (IOException e) {
e.printStackTrace();
}
}
// copy file
public void copyFile(){
byte[] buffer = new byte[1024];
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream("/home/leo/Java/io.txt");
outputStream = new FileOutputStream("/home/leo/Java/io.txt.copy1");
int numberRead = inputStream.read(buffer);
while (numberRead != -1){
outputStream.write(buffer, 0, numberRead);
numberRead = inputStream.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// BufferReader,FileInputStream
public void countLines(){
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("/home/leo/Java/io.txt.copy")));
int charCounts = 0;
Long lineCounts = 0L;
// while (reader.read() != -1){
// charCounts += 1;
// }
// 无论是读一个字符还是一行,每read或者readLine一次,读的指针就往前一步
while (reader.readLine() != null){
out.println(reader.readLine());
lineCounts += 1;
}
out.println("charCounts=" + charCounts + " " + "lineCounts=" + lineCounts);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// BufferWriter
public void useBufferWriter(){
try {
FileWriter fileWriter = new FileWriter("/home/leo/Java/io.txt.copy1");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("hello");
bufferedWriter.newLine(); //写入一个换行符
bufferedWriter.write("world");
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java的IO体系包括字符流和字节流,如下图所示:
下面对常见的IO操作举例,如果想要参考IO的其他流操纵,请阅读官方的API文档
import java.io.*;
import static java.lang.System.*;
/**
* Created by leo on 17-7-11.
*/
public class IONote {
public static void main(String[] args){
IONote note = new IONote();
// note.createFile("/home/leo/Java/io.txt");
// note.copyFile();
// note.countLines();
note.useBufferWriter();
}
public void createFile(String fileName){
File file = new File(fileName);
try {
file.createNewFile();
// out.println(file.getTotalSpace()/1024/1024/1024 + "G");
file.mkdirs();
out.println(file.getName());
out.println(file.getParent());
} catch (IOException e) {
e.printStackTrace();
}
}
// copy file
public void copyFile(){
byte[] buffer = new byte[1024];
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream("/home/leo/Java/io.txt");
outputStream = new FileOutputStream("/home/leo/Java/io.txt.copy1");
int numberRead = inputStream.read(buffer);
while (numberRead != -1){
outputStream.write(buffer, 0, numberRead);
numberRead = inputStream.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// BufferReader,FileInputStream
public void countLines(){
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("/home/leo/Java/io.txt.copy")));
int charCounts = 0;
Long lineCounts = 0L;
// while (reader.read() != -1){
// charCounts += 1;
// }
// 无论是读一个字符还是一行,每read或者readLine一次,读的指针就往前一步
while (reader.readLine() != null){
out.println(reader.readLine());
lineCounts += 1;
}
out.println("charCounts=" + charCounts + " " + "lineCounts=" + lineCounts);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// BufferWriter
public void useBufferWriter(){
try {
FileWriter fileWriter = new FileWriter("/home/leo/Java/io.txt.copy1");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("hello");
bufferedWriter.newLine(); //写入一个换行符
bufferedWriter.write("world");
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- [零基础学JAVA]Java SE应用部分-29.Java IO操作(03) 推荐
- JAVA基础------IO操作(一)
- Java 基本的IO操作
- 黑马程序员 java基础IO操作原则随笔
- java基础入门-----list容器的基本操作3
- [零基础学JAVA]Java SE应用部分-30.Java IO操作(04) 推荐
- Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令
- java基础入门-----list容器的基本操作1
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- 『黑马程序员』---java--IO--分段读写-操作基本类型-内存为源
- java IO操作与字节流(八)过滤流输入输出基本数据类型
- Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令
- JAVA基础------IO操作(三)
- 黑马程序员-java基础-IO学习笔 4000 记
- [零基础学JAVA]Java SE应用部分-27.Java IO操作(01) 推荐
- 2011-08-30 交作业(java io 文件基本操作)
- Java中IO操作的基本规律
- java基础之IO操作
- 学习笔记之javaweb基础:jsp基本语法,内置对象,对JavaBean的操作语法
- java基础入门-----list容器的基本操作2