java中IO简介
java中IO简介
高级语言中最能概念上能和硬件联系起来的就是io了吧…简单介绍下java中的io处理
纯原创,还是大二学生,总结的不到位的地方也请给位指教
转载请指明出处
io简介
- 什么io
io是计算机的输入和输出,控制计算机数据流的走向
- io的对象 文件,网络,终端
-
流(Stream)的概念
一连串流动的字节/字符
以先进先出方式发送信息的通道 -
流的分类
字节流(Byte Stream)
字符流(Character Stream)
字节流和字符流
-
Byte stream
-
java.io.InputStream
int read()//read a byte -
java.io.OutputStream
write(int b)
-
-
Character stream
-
java.io.Reader
int read() -
java.io.Writer
write(int b)
-
-
字节流和字符流的差别
-
字节流
每次往OutputStream里写时,内容应为int或byte[]; 每次从InputStream里读时,读出的是int或byte[]; -
字符流 每次往Writer里写时,内容应为char[]、int或String;
每次从Reader中读时,读出的是int或char[]或CharBuffer;
-
-
缓冲流(Buffered Stream) 为输入输出流增加buffer
- read()和write()方法并丌一定是在调用时立刻执行,而是在buffer满了后执行,以提高输入输出的效率
- buffer是用内部的数组实现的
- 缓冲流常常套在其他的流上(例如FileInputStream等),为其他的流提供缓冲功能
- flush()方法
- 通常使用缓冲流可以使得程序性能大幅上升
java中io包的介绍
java.io.File - “A Path in a file system”
构造方法
File file=new File(“d:/快播);
File file = new File(“d:/”,”快播”);
File file = new File(“.”);
File类提供文件及目录的操作(不包括创建文件),但不能读写文件内容
file类的方法
isFile(), isDirectory(), exists()
delete(), createNewFile(), mkdir()
list(), listFiles()
java.io.RandomAccessFile //随即存取,同c++
- RandomAccessFile往往存储定长的记录(二进制)
- 通过seek(long postion)来定位
- 与InputStream和OutputStream没有关系
- 实现了部分DataInputStream和DataOutputStream的功能
- 常用于构建搜索引擎的索引文件
java.io.PrintWriter
可以创建文件并向文本文件中数据
PrintWriter output = new PrintWriter(file);
output.print(“”);
output.println(“”)
如果文件不存在,会自动创建该文件
字节流IO类的继承关系
一回我去试试上图,刚开始用markdown 不是很习惯…
FileInputStream和FileOutputStream
FileOutputStream(file,append)//构造器中的参数可以是file对象,可以是文件路径,这个比较特殊
FileOutputStream output = new FileOutputStream(file,true);//写在文件尾
output.write()写入字节
FileInputStream类似
input.read()返回int类型,-1结束
缺点:只能读字节,string double等类型都不行
DataInputStream和DataOutputStream
从数据流中读取字节,并转为正确的格式
new DataInputStream(new FileInputStream(“快播.txt“);//需要用字节流对象来初始化
writeUTF(s);//写入字符串
读到文件尾的时候会抛出EOFException 可以用来判断文件尾巴
好吧,没想到异常处理可以这么用…..
BufferedInputStream和BufferedOutputStream类
减少对文件的读写次数从而加快读写速度
初始化
BufferedInputStream(new InputStream(“file”));
BufferedInputStream(filename,bufferSize:int);//指定文件名和缓冲区大小
BufferedOutputStram与此类似
输入输出和DataInputStream方法一样
ObjectInputStream和ObjectOutputStream
可以输入输出对象,同时包含DataInputStream和DataOutputStream所有功能
所以一般用这个代替DataInputStream和DataOutputStream
构造函数和DataInputStream和DataOutputStream一样,比较特殊的输入输出对象的函数
writeObject();
readObject();
FileReader
从文本文件中获取输入字符
FileWriter 将字符写入文本文件
经常使用的类
- PrintStream的继承OutputStream
- DataInputStream和DataOutputStream的用法 继承InputStream和OutputStream
- PrintWriter的用法 继承Writer
-
Scanner的用法 java.util.Scanner
- 附 用scanner写的文件读写
`
/**
* @param args
*/
public Test(){
mark = new double[5];
charFromTxt = new ArrayList< Character >();
stringFromTxt = new ArrayList< String >();
charFromTxtWithContain = new ArrayList< Character >();
}
private double[] mark;
private ArrayList< Character > charFromTxt;
private ArrayList< String > stringFromTxt;
private ArrayList< Character > charFromTxtWithContain;
public void readArraysFromInput(){
Scanner input = new Scanner(System.in);
System.out.println("");
for(int i=0;i<5;i++){
mark[i] = input.nextDouble();
}
Arrays.sort(mark);
System.out.println(Arrays.toString(mark));
}
public void readArraysFromTxt() throws IOException{
File file = new File("20769-0.txt");
FileInputStream input = new FileInputStream(file);
try{
int temp = input.read();
while(temp != -1 ){
charFromTxt.add((char)temp);
temp = input.read();
}
}catch(IOException e){
System.out.print(e);
}
System.out.print(charFromTxt);
input.close();
}
public void readArraysFormTxtWithContain() throws IOException{
File file = new File("20769-0.txt");
FileInputStream input = new FileInputStream(file);
try{
int temp = input.read();
while(temp != -1){
if(!charFromTxtWithContain.contains((char) temp))
charFromTxtWithContain.add((char)temp);
temp = input.read();
}
}catch(IOException e){
System.out.print(e);
}
System.out.print(charFromTxtWithContain);
input.close();
}
public void readArraysStringFormTxt() throws FileNotFoundException{
File file = new File("20769-0.txt");
Scanner input = new Scanner(file);
while(input.hasNext()){
String temp = input.next();
if(!stringFromTxt.contains(temp)){
stringFromTxt.add(temp);
}
}
System.out.println(stringFromTxt.size());
System.out.print(stringFromTxt);
input.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test test = new Test();
test.readArraysFromInput();
test.readArraysFromTxt();
test.readArraysFormTxtWithContain();
test.readArraysStringFormTxt();
}
}