文件
学习目的
编写程序是离不开数据的,数据有多种载体,文件便是其中的一种载体。因此,我们在程序中经常使用的文件。Java把文件封装成了File类,我们通过该类可以对文件进行相关的操作。接下来我们就对文件做详细的介绍。
知识点
- 创建
- I/O流
2.1 流的方向
2.2 输出流
2.3 输入流
2.4 对象
2.5 方法 - 输入数据
3.1 向文件输入数据
3.2 向文件里面存一个对象 - 输出数据
4.1 向文件读取数据
4.2 向文件读取对象 - 将一个文件copy到另外一个位置
解析
- 创建
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建文件 完整路径
// D:\Android\day7\lib\src\main\java\swu\xujiangtao\lib\day8
String path = "D:/Android/day7/lib/src/main/java/swu/xujiangtao/lib/day8";
File file = new File(path.concat("/1.txt"));
//判断是否存在
if (file.exists() == false) {
file.createNewFile();
}
异常处理:throws IOException
- I/O流
2.1 流的方向:参考的是自己的内存空间
2.2 输出流:从内存空间间数据写到外部设备(磁盘、硬盘、光盘)
2.3 输入流:将外部数据写到内存中
流:封装数据的写入和读取
输出流:开发者只需要将内存里面的内容写到流里面(OutputSteam字节流 Writer字符流)
输入流:或者从流里面读取数据(InputSteam字节流 Reader字符流)
2.4 对象
I/O流对象,不属于内存对象,需要自己关闭
2.5 方法
FileOutputSteam\FileInputSteam对文件
ObjectOutputSteam\ObjectInputSteam对对象
Writer\Reader相同
I/O
- 输入数据
3.1 向文件输入数据
//1.创建对象
FileOutputStream fos=new FileOutputStream(file);
//2.调用方法写入
byte[]text={'1','2','3','4'};
fos.write(text);
//3.关闭对象
fos.close();
// 向文件写入数据-字符流
FileWriter fw = new FileWriter(file);
char[] name = {'安','卓','开','发'};
fw.write(name);
fw.close();
3.2 向文件里面存一个对象
//序列化 serializable
//保存的对象必须实现serializable接口
//如果一个类内部还有其他属性变量(如类),那么也要将其实现serializable接口
Dog wc=new Dog();
wc.name="旺财";
person xw=new person();
xw.name="小王";
xw.age=20;
xw.dog=wc;
OutputStream os =new FileOutputStream(file);
ObjectOutputStream oss=new ObjectOutputStream(os);
oss.writeObject(xw);
oss.close();
import java.io.Serializable;
public class person implements Serializable {
public String name;
public int age;
public Dog dog;
}
class Dog implements Serializable{
String name;
}
- 输出数据
4.1 向文件读取数据
FileInputStream fis=new FileInputStream(file);
byte[] name=new byte[12];
int count =fis.read(name);
fis.close();
System.out.println(new String(name));
FileReader fr = new FileReader(file);
char[] book = new char[4];
count = fr.read(book);
fr.close();
System.out.println(count+" "+new String(book));
4.2 向文件读取对象
InputStream is=new FileInputStream(file);
ObjectInputStream iss=new ObjectInputStream(is);
person xw=(person)iss.readObject();
System.out.println(xw.name+" "+xw.age+" "+xw.dog.name);
iss.close();
- 将一个文件copy到另外一个位置
//1.源文件路径
String sourcePath="C:\\Users\\MACHENIKE\\Desktop\\QQ.jpg";
//2.目标文件路径
String desPath="D:\\Android\\day7\\lib\\src\\main\\java\\swu\\xujiangtao\\lib\\day8\\QQ.jpg";
//3.图片 字节
FileInputStream fis=new FileInputStream(sourcePath);
FileOutputStream fos=new FileOutputStream(desPath);
byte[] in=new byte[1024];
while (true){
int count=fis.read(in);
if (count!=-1){
//读到内容
//将这一次数据写到目标文件
fos.write(in,0,count);
}else {
break;
1
}
}
fis.close();
fos.close();
*/
String sourcePath="C:\\Users\\MACHENIKE\\Desktop\\QQ.jpg";
String desPath="D:\\Android\\day7\\lib\\src\\main\\java\\swu\\xujiangtao\\lib\\day8\\Q.jpg";
//输入流
InputStream is=new FileInputStream(sourcePath);
BufferedInputStream bis=new BufferedInputStream(is);
//输出流
OutputStream os=new FileOutputStream(desPath);
BufferedOutputStream bos=new BufferedOutputStream(os);
byte[] in=new byte[1024];
int count=0;
while ((count=bis.read(in))!=-1){
bos.write(in,0,count);
}
bis.close();
bos.close();
}