六,IO系统

六,IO系统

一,数据源

1,数据源--管道确认使用那根管道--节点流

2,先确定管道在tey中new出管道,new出后就写关闭代码,写完关闭代码在写中间代码

3,取数据和放数据结束语句必须有两个,不能写在一个try中

4,fout.flush();//冲刷,用来强制执行完后才能执行下一步

5,对象的序列化和反序列化;

1,将输入的二进制对象流转换为对象(不管从哪来)

2,把对象以二进制流的形式输出(不管输出到哪)

3,transient();修饰的属性值不参与序列化

二,字节流(byte)二进制数据,我们看不懂,什么数据都可以传,包括字符流

1,字节输入流;(inputstream),字节输出流;(outputstream)

例;

FileInputStream fin = null;

FileOutputStream fout = null;

try {

fin = new FileInputStream("D:/lileihanmeimei.mp3");

fout = new FileOutputStream("F:/my.mp3");

byte[] b = new byte[1024];

int length = 0;

while((length = fin.read(b)) != -1){

fout.write(b,0,length);

fout.flush();//冲刷

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fin != null){

try {

fin.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(fout != null){

try {

fout.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2,对象反序列化:(ObjectOutputStream)将输入的二进制对象流转换为对象

例;

StudentBean stu = new StudentBean("小白", 25);

stu.setHomeAddress(new AddressBean("中国", "四川", "绵阳"));

ObjectOutputStream oos = null;

try {

oos = new ObjectOutputStream(new FileOutputStream("stu.data"));

oos.writeObject(stu);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(oos != null){

try {

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3,对象序列化(ObjectInputStream)---把对象以二进制流的形式输出

例;

StudentBean stu = null;

ObjectInputStream ois = null;

try {

ois = new ObjectInputStream(new FileInputStream("stu.data"));

stu = (StudentBean)ois.readObject();

stu.study();

System.out.println(stu.getAge());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally{

if(ois != null){

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

三,字符流(char)文本数据,我们看得懂的,只能传字符流

1,字符输入流(reader)

例;

String poem = "";

FileReader fr = null;

try {

fr = new FileReader("poem.txt");

int c = 0;

while((c = fr.read()) != -1){

poem += (char)c;//取字符

}

System.out.println(poem);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fr != null){

try {

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

2,字符输出流(writer)

例;

String poem = "床前明月光,地上疑似霜。";

1、确定管道

FileWriter fw = null;

try {

fw = new FileWriter("poem.txt");

2、操作管道

fw.write(poem);//放字符

} catch (IOException e) {

e.printStackTrace();

} finally{

3、关闭管道

if(fw != null){

try {

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

四,file来自io包是Java中专门用于封装表示文件/文件夹的类型

File是Java中专门用来封装表示文件/文件夹的类型

1,File表示文件

File file = new File("F:/my.mp3");

ile file1 = new File("F:/hello.txt");

File file2 = new File("D:/world.txt");

System.out.println(file.getName());

System.out.println(file.getPath());

System.out.println(file.getParent());

System.out.println(file.length());

System.out.println(new Date(file.lastModified()));

try {

if(!file1.exists()){

file1.createNewFile();

}else{

file1.renameTo(file2);

}

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(file1.canWrite());

System.out.println(file1.isHidden());

2,File表示文件夹

File dir = new File("D:/tools");

if(dir.isDirectory()){

String[] subStrs = dir.list();

for(String sub : subStrs){

System.out.println(sub);

}

File[] subFiles = dir.listFiles();

for(File sub : subFiles){

if(sub.isHidden()){

System.out.println(sub.getName());

}

}

}

六,IO系统

一,数据源

1,数据源--管道确认使用那根管道--节点流

2,先确定管道在tey中new出管道,new出后就写关闭代码,写完关闭代码在写中间代码

3,取数据和放数据结束语句必须有两个,不能写在一个try中

4,fout.flush();//冲刷,用来强制执行完后才能执行下一步

5,对象的序列化和反序列化;

1,将输入的二进制对象流转换为对象(不管从哪来)

2,把对象以二进制流的形式输出(不管输出到哪)

3,transient();修饰的属性值不参与序列化

二,字节流(byte)二进制数据,我们看不懂,什么数据都可以传,包括字符流

1,字节输入流;(inputstream),字节输出流;(outputstream)

例;

FileInputStream fin = null;

FileOutputStream fout = null;

try {

fin = new FileInputStream("D:/lileihanmeimei.mp3");

fout = new FileOutputStream("F:/my.mp3");

byte[] b = new byte[1024];

int length = 0;

while((length = fin.read(b)) != -1){

fout.write(b,0,length);

fout.flush();//冲刷

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fin != null){

try {

fin.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(fout != null){

try {

fout.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2,对象反序列化:(ObjectOutputStream)将输入的二进制对象流转换为对象

例;

StudentBean stu = new StudentBean("小白", 25);

stu.setHomeAddress(new AddressBean("中国", "四川", "绵阳"));

ObjectOutputStream oos = null;

try {

oos = new ObjectOutputStream(new FileOutputStream("stu.data"));

oos.writeObject(stu);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(oos != null){

try {

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3,对象序列化(ObjectInputStream)---把对象以二进制流的形式输出

例;

StudentBean stu = null;

ObjectInputStream ois = null;

try {

ois = new ObjectInputStream(new FileInputStream("stu.data"));

stu = (StudentBean)ois.readObject();

stu.study();

System.out.println(stu.getAge());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally{

if(ois != null){

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

三,字符流(char)文本数据,我们看得懂的,只能传字符流

1,字符输入流(reader)

例;

String poem = "";

FileReader fr = null;

try {

fr = new FileReader("poem.txt");

int c = 0;

while((c = fr.read()) != -1){

poem += (char)c;//取字符

}

System.out.println(poem);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fr != null){

try {

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

2,字符输出流(writer)

例;

String poem = "床前明月光,地上疑似霜。";

1、确定管道

FileWriter fw = null;

try {

fw = new FileWriter("poem.txt");

2、操作管道

fw.write(poem);//放字符

} catch (IOException e) {

e.printStackTrace();

} finally{

3、关闭管道

if(fw != null){

try {

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

四,file来自io包是Java中专门用于封装表示文件/文件夹的类型

File是Java中专门用来封装表示文件/文件夹的类型

1,File表示文件

File file = new File("F:/my.mp3");

ile file1 = new File("F:/hello.txt");

File file2 = new File("D:/world.txt");

System.out.println(file.getName());

System.out.println(file.getPath());

System.out.println(file.getParent());

System.out.println(file.length());

System.out.println(new Date(file.lastModified()));

try {

if(!file1.exists()){

file1.createNewFile();

}else{

file1.renameTo(file2);

}

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(file1.canWrite());

System.out.println(file1.isHidden());

2,File表示文件夹

File dir = new File("D:/tools");

if(dir.isDirectory()){

String[] subStrs = dir.list();

for(String sub : subStrs){

System.out.println(sub);

}

File[] subFiles = dir.listFiles();

for(File sub : subFiles){

if(sub.isHidden()){

System.out.println(sub.getName());

}

}

}

 

原文地址:https://www.cnblogs.com/zpcbk/p/6487091.html