文件
學(xué)習(xí)目的
編寫程序是離不開數(shù)據(jù)的,數(shù)據(jù)有多種載體襟铭,文件便是其中的一種載體。因此短曾,我們?cè)诔绦蛑薪?jīng)常使用的文件寒砖。Java把文件封裝成了File類,我們通過該類可以對(duì)文件進(jìn)行相關(guān)的操作嫉拐。接下來我們就對(duì)文件做詳細(xì)的介紹入撒。
知識(shí)點(diǎn)
- 創(chuàng)建
- I/O流
2.1 流的方向
2.2 輸出流
2.3 輸入流
2.4 對(duì)象
2.5 方法 - 輸入數(shù)據(jù)
3.1 向文件輸入數(shù)據(jù)
3.2 向文件里面存一個(gè)對(duì)象 - 輸出數(shù)據(jù)
4.1 向文件讀取數(shù)據(jù)
4.2 向文件讀取對(duì)象 - 將一個(gè)文件copy到另外一個(gè)位置
解析
- 創(chuàng)建
public static void main(String[] args) throws IOException, ClassNotFoundException {
//創(chuàng)建文件 完整路徑
// 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 流的方向:參考的是自己的內(nèi)存空間
2.2 輸出流:從內(nèi)存空間間數(shù)據(jù)寫到外部設(shè)備(磁盤、硬盤椭岩、光盤)
2.3 輸入流:將外部數(shù)據(jù)寫到內(nèi)存中
流:封裝數(shù)據(jù)的寫入和讀取
輸出流:開發(fā)者只需要將內(nèi)存里面的內(nèi)容寫到流里面(OutputSteam字節(jié)流 Writer字符流)
輸入流:或者從流里面讀取數(shù)據(jù)(InputSteam字節(jié)流 Reader字符流)
2.4 對(duì)象
I/O流對(duì)象茅逮,不屬于內(nèi)存對(duì)象璃赡,需要自己關(guān)閉
2.5 方法
FileOutputSteam\FileInputSteam對(duì)文件
ObjectOutputSteam\ObjectInputSteam對(duì)對(duì)象
Writer\Reader相同
I/O
- 輸入數(shù)據(jù)
3.1 向文件輸入數(shù)據(jù)
//1.創(chuàng)建對(duì)象
FileOutputStream fos=new FileOutputStream(file);
//2.調(diào)用方法寫入
byte[]text={'1','2','3','4'};
fos.write(text);
//3.關(guān)閉對(duì)象
fos.close();
// 向文件寫入數(shù)據(jù)-字符流
FileWriter fw = new FileWriter(file);
char[] name = {'安','卓','開','發(fā)'};
fw.write(name);
fw.close();
3.2 向文件里面存一個(gè)對(duì)象
//序列化 serializable
//保存的對(duì)象必須實(shí)現(xiàn)serializable接口
//如果一個(gè)類內(nèi)部還有其他屬性變量(如類),那么也要將其實(shí)現(xiàn)serializable接口
Dog wc=new Dog();
wc.name="旺財(cái)";
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;
}
- 輸出數(shù)據(jù)
4.1 向文件讀取數(shù)據(jù)
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 向文件讀取對(duì)象
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();
- 將一個(gè)文件copy到另外一個(gè)位置
//1.源文件路徑
String sourcePath="C:\\Users\\MACHENIKE\\Desktop\\QQ.jpg";
//2.目標(biāo)文件路徑
String desPath="D:\\Android\\day7\\lib\\src\\main\\java\\swu\\xujiangtao\\lib\\day8\\QQ.jpg";
//3.圖片 字節(jié)
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){
//讀到內(nèi)容
//將這一次數(shù)據(jù)寫到目標(biāo)文件
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();
}