刚开始学java IO的时候,有好多不明白的地方,入行后工作中真正用到的地方也不是太多,有一天忽然想回过头来,在看看自己对基础java的认识,才发现自己竟然有好多东西已经忘却了,所以写下这个例子,再从新学一遍java IO
package com.leadron.test1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.StringTokenizer;
public class ByteIOTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ByteIOTest t = new ByteIOTest();
t.fileTest();
}
// ===========================================================================
/**
*
* 字节流的操作
*
*/
// ===========================================================================
// 字节类型的读取
/**
* input stream 1)ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
* 2)StringBufferInputStream:把一个String对象作为InputStream
* 3)FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
* 4)PipedInputStream:实现了pipe的概念,主要在线程中使用
*
* out stream 1)ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
* 2)FileOutputStream:把信息存入文件中 3)PipedOutputStream:实现了pipe的概念,主要在线程中使用
* 4)SequenceOutputStream:把多个OutStream合并为一个OutStream
*/
/**
* ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用
* ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。
* 在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,
* 然后一次性把数据发送出去。具体用法如下:
*
* ByteArrayOutputStream: 可以捕获内存缓冲区的数据,转换成字节数组。
*
* ByteArrayInputStream: 可以将字节数组转化为输入流
*
* 如下所示:ByteArrayOutputStream把内存中的数据读到字节数组中,
* 而ByteArrayInputStream又把字节数组中的字节以流的形式读出,实现了对同一个字节数组的操作.
*/
public void byteRead() {
int a = 0;// 把数据放在内存中的缓冲区
int b = 1;
int c = 2;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.write(a);// 捕获内存缓冲区的数据
bout.write(b);
bout.write(c);
byte[] buff = bout.toByteArray();// 转换成字节数组
for (int i = 0; i < buff.length; i++) {
System.out.println(buff[i]);
}
System.out.println("***********************");
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
while ((b = bin.read()) != -1) {
System.out.println(b);
}
try {
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stringBufferInputStreamTest() {
String str = "11111111111111\n333333333333333333\nadbcd";
int s;
StringBufferInputStream bin = new StringBufferInputStream(str);
while ((s = bin.read()) != -1) {
System.out.println(s);
}
}
public void fileInputStreamTest() {
FileInputStream bin = null;
FileOutputStream fw=null;
File fl=null;
try {
fl=new File("src/com/leadron/test1/a.txt");
if(!fl.exists()){
fl.createNewFile();
}
byte b=1;
fw=new FileOutputStream(fl);
OutputStreamWriter ow=new OutputStreamWriter(fw);
ow.write("我们的");
ow.write(2);
ow.flush();
ow.close();
bin = new FileInputStream(fl);
try {
System.out.println(bin.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// ===========================================================================
/**
*
* Unicode字符为导向的stream的操作
*
*
* 1 Input Stream
* 1) CharArrayReader:与ByteArrayInputStream对应
* 2) StringReader:与StringBufferInputStream对应
* 3) FileReader:与FileInputStream对应
* 4) PipedReader:与PipedInputStream对应
*
*
* 2 Out Stream
* 1) CharArrayWriter:与ByteArrayOutputStream对应
* 2) StringWriter:无与之对应的以字节为导向的stream
* 3) FileWriter:与FileOutputStream对应
* 4) PipedWriter:与PipedOutputStream对应
*
*
*
*/
// ===========================================================================
public void charArrayReader() {
int a = 1;
int b = 2;
int c = 3;
CharArrayWriter br = null;
br = new CharArrayWriter();
br.write(a);
br.write(b);
br.write(c);
char[] but = br.toCharArray();
CharArrayReader bin = null;
bin = new CharArrayReader(but);
try {
while ((c = bin.read()) != -1) {
System.out.println((char)c);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
br.close();
bin.close();
}
}
public void stringReader() {
StringReader bin = null;
StringWriter bw = null;
int a;
bw = new StringWriter(10);
bw.write("1");
bw.write("2");
StringBuffer buf = bw.getBuffer();
bin = new StringReader(buf.toString());
try {
while ((a = bin.read()) != -1) {
System.out.println((char)a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bin.close();
}
}
public void fileReader() {
FileReader fr = null;
try {
File fl = new File("src/com/leadron/test1/file1.txt");
if (!fl.exists()) {
fl.createNewFile();
}
fr = new FileReader(fl);
int a;
while ((a = fr.read()) != -1) {
System.out.println((char)a);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// ==========================================================================
/**
* 以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现
* 的功能相同,字是在操作时的导向不同。如CharArrayReader:和ByteArrayInputStream的
* 作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取 一个字节的信息,
* 而后者每次从内存中读取一个字符。
*/
// ==========================================================================
// ===========================================================================
/**
*
* InputStreamReader和OutputStreamWriter:把一个以字节为导向的stream转换成
* 一个以字符为导向的stream。
*
*/
// ===========================================================================
public void inputStreamReader(){
try
{
int b;
InputStreamReader isr = new InputStreamReader (new FileInputStream (new File ("src/com/leadron/test1/file2.txt")));
System.out.println ("The content of text is:");
while ((b = isr.read()) != -1)//顺序读取文件text里的内容并赋值给整型变量b,直到文件结束为止。
{
System.out.print((char)b);
}
isr.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
public void outputStreamWriter(){
File fl=new File("src/com/leadron/test1/file2.txt");
if(!fl.exists()){
try {
fl.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fl.exists()){
FileOutputStream op=null;
try {
op = new FileOutputStream(fl);
//如果直接用字节写中文的话,会产生乱码
OutputStreamWriter ow= new OutputStreamWriter(op);
ow.write("大家好,我正在学习Java \n");
ow.write("请多多指教");
ow.write(1);
System.out.println("file2.txt写入成功!***************开始读..\n");
ow.flush();
ow.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// ===========================================================================
// 简单文件读写
public void fileTest() {
try {
File f = new File("src/com/leadron/test1/file1.txt");
if (!f.exists()) {
f.createNewFile();
}
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("大家好,我正在学习Java");
bw.newLine();
bw.write("请多多指教");
System.out.println("file1.txt写入成功!***************开始读..\n");
bw.flush();
bw.close();
FileReader fr = new FileReader("file1.txt");
BufferedReader br = new BufferedReader(fr);
String temp = null;
do {
temp = br.readLine();
System.out.println(temp == null ? "" : temp);
} while (temp != null);
fr.close();
br.close();
System.out.println("file1.txt已经读完!*************");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
//创建一个类
public void creatJava(){
FileWriter fw=null;
BufferedWriter bw = null;
try {
File fjava=new File("src/com/leadron/test1/TestCreate.java");
if(!fjava.exists()){
fjava.createNewFile();
}
fw=new FileWriter(fjava);
bw = new BufferedWriter(fw);
bw.write("package com.leadron.test1;");
bw.newLine();
bw.write("public class TestCreate implements Serializable{");
bw.newLine();
bw.write(" public static void main(String[] args){");
bw.newLine();
bw.write(" System.out.println(\"Hello World !\");");
bw.newLine();
bw.write(" }");
bw.newLine();
bw.write("}");
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// File的操作
public void fileTest2() {
try {
File f = new File("file1.txt");
System.out.println("文件是否存在: " + f.exists());
if (!f.exists()) {
System.out.println("文件不否存在,开始创建!");
f.createNewFile();
}
System.out.println("文件是否存在: " + f.exists());
System.out.println("是文件吗: " + f.isFile());
System.out.println("是文件夹吗: " + f.isDirectory());
System.out.println("可否读取文件: " + f.canRead());
System.out.println("可否修改文件: " + f.canWrite());
System.out.println("是否隐藏: " + f.isHidden());
System.out.println("文件名称: " + f.getName());
System.out.println("标准文件名: " + f.getCanonicalFile());
System.out.println("相对路径: " + f.getPath());
System.out.println("绝对路径: " + f.getAbsolutePath());
System.out.println("标准路径: " + f.getCanonicalPath());
System.out.println("最后修改时间: " + f.lastModified());
System.out.println("文件大小: " + f.length() + " 字节");
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 分隔符读取
public void fileTest3() {
try {
File f = new File("file1.txt");
if (!f.exists()) {
f.createNewFile();
}
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("小明,男,1980-1-1,13624577654");
bw.newLine();
bw.write("小强,男,1984-2-1,13634375634");
bw.newLine();
bw.write("小红,女,1986-1-5,13724777774");
System.out.println("数据写入成功!");
bw.flush();
bw.close();
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = null;
while ((s = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s, ",");
System.out.println(s == null ? " " : s);
while (st.hasMoreTokens()) {
String name = st.nextToken();
String sex = st.nextToken();
String birthday = st.nextToken();
String tel = st.nextToken();
System.out.println("姓名: " + name);
System.out.println("性别: " + sex);
System.out.println("生日: " + birthday);
System.out.println("电话: " + tel);
System.out.println("_______________________________ ");
}
}
while (s != null)
;
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void fileTest4() {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
String name = "xxy";
int age = 84;
try {
dout.writeUTF(name);
dout.writeInt(age);
byte[] buff = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
DataInputStream dis = new DataInputStream(bin);
String newName = dis.readUTF();
int newAge = dis.readInt();
System.out.println(newName + ":" + newAge);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}