IO文件处理

1.文件的基本操作:创建、删除、获取路径、获取名字。。。API里面全部都有
创建文件,文件夹是目录,不是文件(异常):

File f = new File("C:\\Users\\Admin\\Desktop/file.txt");
try {
  //做什么
f.createNewFile();
} catch (Exception e) {
  //异常了做什么
e.printStackTrace();
}

2.文件的复制:
  ①获得文件内容(读出来)→②在新的路径创建文件→③将内容复制进去(写进去)
  InputStream输入流和OutputStream输出流都是抽象超类,要用他们的子类来实现:FileInputStream和FileOutputStream
  每次读半个汉字,一个数字或字母

public class MyFile {
public void filecopy(){
try {
//创建文件输入流,old.txt需要原来就存在
FileInputStream fis = new
FileInputStream("H:\\old.txt");
//创建文件输出流,new.txt是自动创建的
FileOutputStream fos = new
FileOutputStream("H:\\new.txt");

//!!!!!!!!!!!!!!!!!!!!!!!!!!!
//需要从文件中读取数据,如果读到文件的末位则为read()返回值为-1
int value = fis.read();
//循环读取字节
while(value!=-1){
fos.write(value);
value = fis.read();
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//强制输出
fos.flush();
//关闭输入输出流
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
  }

.doc复制成.txt后会乱码
3.文件的打开和保存
假设要保存的是L-System的数据,它由多个部分组成比如很多点,需要输入的序列码,还有暂停时间,角度等。而且需要保存或打开的不止一个,则需要数组或队列
①静态static:可以再类外部直接调用
  ②保存函数和读取函数
③用到了DataInputStream和DataOutputStream
④字符串的处理:字符串是不定长,所以处理的时候要先得到他的长度才可以

/**
* 执行文件保存的函数
* @param ls 传进来需要保存的函数
*/
public static void save(ArrayList<Lsystem> ls){
String fileName = "H:\\ioFile.abc";
byte version = 1;
int size = ls.size();//L-System的个数
try {
FileOutputStream fs = new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(fs);
dos.writeByte(version);//将版本号写入文件的头部
dos.writeInt(size);//保存的个数
for(int i = 0;i<ls.size();i++){//循环读入对象的数据
Lsystem s = ls.get(i);//将对象传进去
dos.writeInt(s.count);//将各个参数穿进去进行保存
dos.writeDouble(s.d);
dos.writeLong(s.t);
//处理字符串:字符串是不知道它具体有多少位的,所以就要定义它的长度
byte[] ss = s.s.getBytes();
dos.writeInt(ss.length);
dos.write(ss);
//处理自定义类
int point_size = s.p.length;//每个Lsystem拥有的点的个数
dos.writeInt(point_size);
for(int j = 0;j<point_size;j++){
dos.writeInt(s.p[j].x);
dos.writeInt(s.p[j].y);
}
}
dos.flush();
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* 读取文件
* @return 返回的是一个得到的是保存的数据
*/
public static ArrayList<Lsystem> getData(){
String fileName = "H:\\ioFile.abc";
ArrayList<Lsystem> L_array = new ArrayList<Lsystem>();
try {
FileInputStream fis = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fis);
byte version = dis.readByte();
int size = dis.readInt();
for(int i = 0;i<size;i++){
int count = dis.readInt();//将各个参数读出来
double d = dis.readDouble();
long t = dis.readLong();
//读取字符串
int slength = dis.readInt();
byte[] ss = new byte[slength];
for(int j = 0;j<slength;j++){
ss[j] = dis.readByte();
}
String s = new String(ss);
//读取自定义类
int point_size = dis.readInt();//点的个数
Point[] p = new Point[point_size];
for(int j = 0;j<point_size;j++){
p[j].x = dis.readInt();
p[j].y = dis.readInt();
}
Lsystem ls = new Lsystem();
ls.count = count;
ls.p = p;
ls.d = d;
ls.s = s;
ls.t = t;

//将对象添加到队列中去
L_array.add(ls);
}
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return L_array;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值