码迷,mamicode.com
首页 > 其他好文 > 详细

IO流的基础与小型项目

时间:2018-07-30 21:44:27      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:trace   case   student   switch   set   文件路径   []   vat   技术   

? IO流

对于流的描述,流是一种抽象的描述。

流的分类:

1、输入流(Input)

2、输出流(Output)

 

按类型分:

1、字节流(InputStream/OutputStream)

public class ReadFileByLoop {
?
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
?
// 根据给定的文件路径构建一个字节输入流对象
File f = new File("C:\\Users\\lenovo\\Desktop\\培训\\试卷及答案\\1.txt");
InputStream is = new FileInputStream(f);
// 准备一个自己缓冲区
byte[] b = new byte[1024];
// 申明一个临时变量记录本次读取的真实长度
int len = 0;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len);
System.out.println(s);
}
?
// is.close();
}
?
}

 

2、字符流(Reader/Writer)


public class ReadFileByChar {
?
public static void main(String[] args) throws IOException {
//创建一个File对象
File f = new File("C:\\Users\\lenovo\\Desktop\\培训\\试卷及答案\\1.txt");
//构建一个字符输入流
FileReader fr = new FileReader(f);
char[] a=new char[1024];
int len;
while((len=fr.read(a))!=-1){
String s=new String(a,0,len);
System.out.println(s);
}
fr.close();
}
}

 

按照功能分:

1、节点流(低级流)

2、处理流(高级流)

字节流到字符流之间的流动用InputStreamReader

BufferedReader x=new BufferedReader(new InputStreamReader(System.in))

字符流到字符流之间的流动用OutputStreamWriter

 

 

对一个文件夹拷贝


public class Test {
?
// 递归方法
public static void copyFile(File file, File file2) {
// 当找到目录时,创建目录
if (file.isDirectory()) {
file2.mkdir(); // 创建目录
File[] files = file.listFiles();
for (File f : files) {
// 递归
copyFile(f, new File(file2, f.getName()));
}
// 当找到文件时
} else if (file.isFile()) {
File f = new File(file2.getAbsolutePath());
try {
f.createNewFile();
copyDatas(file.getAbsolutePath(), f.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
?
// 复制文件数据的方法
public static void copyDatas(String filePath, String filePath1) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 字节流
fis = new FileInputStream(filePath);
fos = new FileOutputStream(filePath1);
byte[] buffer = new byte[1024];
while (true) {
int temp = fis.read(buffer, 0, buffer.length);
if (temp == -1) {
break;
} else {
fos.write(buffer, 0, temp);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
?
public static void main(String args[]) {
File file = new File("D:\\大学文件管理");
File file2 = new File("D:\\1");
copyFile(file, file2);
}
}

对象序列化

对象序列化是一种用于在文件或者各种其他输入输出设备中存储java对象的机制,通过将实现过序列化结构的对象存储到指定的输出源,可以完整的保存对象数据;对象序列化机制一般常用于大型项目中的缓存技术,以缓存经常需要使用到的对象数据;java中实现对象序列化通常包含两种方式:

  1. 实现Serializable接口

  2. 实现Externalizable接口

完成对象序列化的过程必须包含以下步骤:

  1. 需要完成序列化的对象对应的类必须实现Serializable接口

  2. 通过对象输出流将对象存储到指定的输出源(文件,网络)中(通过ObjectOutputStream)

例子:

输入三个成绩,语数外三个科目的成绩:按照总成绩存到文件夹,在控制台查看


?
public class Student implements Serializable{
?
/**
*
*/
private static final long serialVersionUID = 1L;
private int chinese;
private int math;
private int english;
private int sorce;

public Student() {
// TODO Auto-generated constructor stub
}
public Student(int chinese, int math, int english) {
super();
this.chinese = chinese;
this.math = math;
this.english = english;
this.sorce=chinese+english+math;
}
public int getSorce() {
return sorce;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return "Student 语文=" + chinese + ", 数学=" + math + ", 英语=" + english + ", 总分=" + sorce + "]";
}

}
?
?
?
?
?
?
package com.softeem.Objects;
?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
?
public class StudentTest {
ArrayList<Student> list = new ArrayList<>();
Student stu = new Student();
?
public void scanner() {
Scanner sc = new Scanner(System.in);
System.out.println("语文成绩:");
int a = sc.nextInt();
System.out.println("数学成绩:");
int b = sc.nextInt();
System.out.println("英语成绩:");
int c = sc.nextInt();
Student stu = new Student(a, b, c);
list.add(stu);
System.out.println("添加成功!");
}
?
public void select() {
for (Student s : list) {
System.out.println(s);
}
?
}
?
public void copy() throws IOException, ClassNotFoundException {
list.sort(new Comparator<Student>() {
@Override
public int compare(Student x, Student y) {
// TODO Auto-generated method stub
return x.getSorce() - y.getSorce();
}
});
OutputStream os = new FileOutputStream("ok.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(list);
oos.close();
}
?
private void selectCopy() throws IOException, ClassNotFoundException {
InputStream is = new FileInputStream("ok.txt");
ObjectInputStream ois = new ObjectInputStream(is);
Object obj = ois.readObject();
//ois.readLine()
System.out.println(obj);
}
public void pint() throws ClassNotFoundException, IOException {
System.out.println("----------【1】输入学生成绩-----------");
System.out.println("----------【2】学生成绩查询-----------");
System.out.println("----------【3】将学生信息添加到文件中----");
System.out.println("----------【4】显示文件中的信息--------");
System.out.println("----------【5】退出--------");
Scanner sc = new Scanner(System.in);
System.out.println("请选择:");
int i = sc.nextInt();
switch (i) {
case 1:
scanner();
pint();
break;
case 2:
System.out.println("查询结果如下:");
select();
System.out.println("查询成功");
pint();
case 3:
copy();
pint();
System.out.println("导入成功!");
break;
case 4:
selectCopy();
pint();
break;
case 5:
System.out.println("退出成功!");
break;
default:
System.err.println("输入错误");
}
}
?

?
public static void main(String[] args) throws ClassNotFoundException, IOException {
new StudentTest().pint();
}
}
?

 

IO流的基础与小型项目

标签:trace   case   student   switch   set   文件路径   []   vat   技术   

原文地址:https://www.cnblogs.com/danhua520tuzi/p/9392431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!