最近想换工作,在网上投了几家公司,去面试的时候,好几家面试官都问一些基础的java语法,还要我手写一个赋值文件的程序,这就让我感觉很无力,本来java的基础就不是很好,平时都是用eclipse编程(没办法提示害的),所有大部分类跟方法都记不全,只记得个大概的用法,所以面试的结果就可想而知有多惨了.
看来想找个好工作,看来这java基础还是得补一补的;首先我瞄准的目标就是java的I/O操作.(谁让每次面试,面试官老是问呢).
正文:
对于java的输入输出第一感觉就是类多,很难记住,首先我还是从最基本的开始将吧,在java中,把输入输出包装成一个流,我们可以对流对象操作就可以达到我们的I/O操作需求,其中流根据操作方向的不同分为输入流:InputStream ;和输出流:OutputStream;其中两者之间的区别在于:
InputStream的数据源不在内存中,是把例如文件等数据源中的数据输入到内存中.
OutputStream的作用刚好相反,它的作用就是把内存中的数据源输出去.
但是由于这个两个类都是抽象类,所以我们一般不用其进行java的I/O操作,而java中I/O绝大多数的类都是这两个类的子类,所有我们可以根据我们的需要选择相应的子类进行java的I/O操作;接下来我们先看一下java I/O中的大体的一些类:
FileOutputStream:用于对文件的操作,将内存中的数据通过该流对象输出到制定的文件中.
FileInputStream:用户对文件的操作,与FileOutputStream的作用刚好相反,用户读取制定文件中的数据到内存中.
ByteArrayInputStream,ByteArrayOutputStream,CharArrayInputStream,CharArrayOutputStream:在内存中开辟一个字符/字节数组空间.
BufferInputStream,BufferOutputStream,BufferReader,BufferWriter具有缓存功能.
DataInputStream和DataOutputStream能对基本数据进行读写,例如int char long等
java I/O中根据每次读取的一个一个字节还是两个字节可以把这些流分成两类:
字节流-------包括字节输入流和字节输出流:
字节输入流:InputStream,FileInputStream,ByteArrayInputStream,DataInputStream,BufferInputStream等.
字符输入流:Reader, FileReader,CharArrayReader,BufferReader,InputStreamReader,PipedReader等.
另外还可以分为节点流和处理流:
字节输入流:
字节输出流:
字符输入流:
字符输出流:
(这几张图片转载自:http://blog.youkuaiyun.com/yuebinghaoyuan/article/details/7388059)
下面我们介绍一下一些常用的输入 输出流的使用:
FileOutputStream ----往文件中写数据:
File fileDirectory = new File("/home/lm");
if (!fileDirectory.exists()) {//该文件夹不存在就创建文件夹
fileDirectory.mkdirs();
}
File outputFile = new File(fileDirectory, "test.txt");//创建文件
try {
FileOutputStream out = new FileOutputStream(outputFile);
String dataSource = "hello world!";
byte[] btData =dataSource.getBytes();
out.write(btData);
out.close();//关闭输出流,不然可能会造成内存溢出
} catch (Exception e) {
// TODO: handle exception
}
FileInputStream----往内存中读入数据:
File file = new File("/home/opt/test.txt");//文件路径
String dirPath = file.getParent();
File dirFile = new File(dirPath);
if (!dirFile.exists()){
dirFile.mkdirs();
}
FileInputStream inputStream;
try {
inputStream = new FileInputStream(file);
byte[] inputContent = new byte[1024];
int len = 0;
while (inputStream.read(inputContent)!=-1) {
System.out.println(new String(inputContent));
}
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println("文件没有找到!");
}catch (Exception e) {
System.out.println("对文件时发生错误!");
}
BufferWriter-----使用缓存区,把数据写写进缓冲区,再从缓存区中把数据写进输入流
File fileDir = new File("你的文件路劲");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(fileDir,"outFile.txt");
try {
FileOutputStream outputStream = new FileOutputStream(file,true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));//其中OutputStreamWriter是字节转换输出流,把字节输出流转换成字符输出流
writer.write("\n Today,I'm a new people!");
writer.close();
outputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
BufferReader-----使用缓存去读入数据,写把输入流中的数据放入在缓冲区,然后每次读数据时从缓存去读取,只有当缓冲区的数据不够时,才对磁盘进行真实的读写操作.
try {
FileInputStream foStream = new FileInputStream(file);
BufferedReader buffer = new BufferedReader(new InputStreamReader(foStream)) ;//其中InputStreamReader是字节转换输入流,把字节输入流转换成字符输入流.
String line;
while((line=buffer.readLine())!=null){
System.out.println(line);
}
buffer.close();
foStream.close();
} catch (Exception e) {
// TODO: handle exception
}
FileWriter---------字符文件输出流
try {
FileWriter writer = new FileWriter(file,true);//其中file表示的要操作的文件对象,布尔值为true表示写入文件中的数据以追加的方式添加到文件的最后,如果该值为false或者不手动
设定,那么表示数据以覆盖原有数据的方式写入数据.
writer.write("\naspend at this file!");
writer.close();
} catch (Exception e) {
// TODO: handle exception
}
FileReader-------字符文件输入流
try {
FileReader reader = new FileReader(file);
char[] content = new char[1024];
while(reader.read(content)!=-1){
System.out.println(new String(content));
}
} catch (Exception e) {
// TODO: handle exception
}
-------------------------------------------------------------------------写的比较粗糙,如有错误,多谢指正---------------------------------------------------------------------------