JAVA FILE or I/O学习 - I/O流操作:FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream、InputStreamReader、OutputStreamWriter等

时间:2023-03-09 01:23:58
JAVA FILE or I/O学习 - I/O流操作:FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream、InputStreamReader、OutputStreamWriter等
 public class IOStreamKnow
{
/*********************************文件读写方式:字节流************************************/
/**
* 方式一:基本方式,文件读写方式的基础
*/
public void name()
{
try
{
//创建输入流,输入流用来读取文件字节信息
//参数表示读取的文件对象
FileInputStream input = new FileInputStream(new File("d:/a"));
//创建输入流,将信息进行输出
//参数表示输出的文件目标
FileOutputStream output = new FileOutputStream(new File("e:/a"));
//读取输入流中的信息
//读取的字节
int data; while((data = input.read()) != -1)
{
//将读取的信息通过输出流完成输出
output.write(data);
}
//清空输出流
output.flush();
//关闭输出流
output.close();
//关闭输入流
input.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 文件读写缓冲流:推荐方式一
* 该种方式需要依据FileInputStream为基础
*/
public void name1()
{
try
{
//缓冲字节流依赖于字节流来构建
BufferedInputStream buffInput = new BufferedInputStream(new FileInputStream(new File("d:/back.jpg")));
//缓冲输出流
BufferedOutputStream buffOutput = new BufferedOutputStream(new FileOutputStream(new File("e:/back.jpg")));
int data;
while((data = buffInput.read()) != -1)
{
buffOutput.write(data);
}
buffOutput.flush();
buffOutput.close();
buffInput.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 文件读写缓冲流:推荐方式二:常用方式
* 该种方式需要依据FileInputStream为基础,并且可以自行定制读写的量
*/
public void name2()
{
try
{
FileInputStream input = new FileInputStream(new File("d:/back.jpg"));
FileOutputStream output = new FileOutputStream(new File("e:/back.jpg"));
//缓冲字节数组
byte[] buffer = new byte[1024];
//将输入流读取的文件信息读入到缓冲区中,返回读取的长度
int len;
while((len = input.read(buffer)) != -1)
{ output.write(buffer,0,len);
}
output.flush();
output.close();
input.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 特定文件读取方式:按照指定的数据源类型,进行读取
*/
public void name3()
{
//data可以在读写的时候按照特定的java格式进行数据的读写
try
{
DataInputStream input = new DataInputStream(new FileInputStream(new File("d:/back.jpg")));
DataOutputStream output = new DataOutputStream(new FileOutputStream(new File("e:/back.jpg")));
int data;
while((data = input.read()) != -1)
{
output.write(data);
}
output.flush();
output.close();
input.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/*********************************对象的存储、读入方式:序列化、反序列化************************************/
/**
* 序列化写入(存储)对象数据
* 反序列化读取对象数据
*/
public void name4()
{
User user = new User(1001, "tom"); //通过序列化保存数据
try
{
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("d:/temp.txt")));
output.writeObject(user);
output.flush();
output.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} //通过反序列化对之前保存的数据进行对象的转换
try
{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("d:/temp.txt")));
//读取文件中的对象
User user1 = (User)input.readObject();
input.close();
System.out.println(user1.getUserId());
System.out.println(user1.getUserName());
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
/*********************************文件读写方式:字符流,主要用于读取文本文件************************************/
/**
* 方式二:由字节流衍生而来,字符流读写方式的基础
*/
public void name5()
{
//方式一:基本方式,文件读写方式的基础
try
{
//创建字符输入流,字符流的构建依赖于字节流,InputStreamReadder是字节流通向字符流的桥梁
//不能使用字符流读取使用字节描述的文件信息,如图片,音频文件,视频文件
InputStreamReader reader = new InputStreamReader(new FileInputStream(new File("d:/test.txt")));
//创建字符输出流
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("e:/test.txt")));
int data;
while((data = reader.read()) != -1)
{
writer.write(data);
//System.out.println(data);
}
writer.flush();
writer.close();
reader.close(); } catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 字符缓冲流:由字符流读写方式的基础而来
*/
public void name6()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("d:/集合项目.txt")))); String str = "";
//每次读取一行
while((str = reader.readLine()) != null)
{
System.out.println(str);
}
reader.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d:/my.txt"))));
writer.write("自定义文本");
//换行
writer.newLine();
writer.write("hello niit");
writer.flush();
writer.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}