如果當(dāng)初陪著你的人走遠(yuǎn)了肴敛,那些日子回憶起來(lái)會(huì)模糊嗎?
mark 標(biāo)記
reset 重置
exist 存在
modified 變更
skip 跳過(guò)
flush 沖刷
File類
package z1;
import java.io.File;
public class FileTest {
public static void main(String[] args) throws Exception {
File file =new File("d:/","abc.txt");
if(file.exists()) { //如果文件存在
file.delete();
}
else {
file.createNewFile();
System.out.println("文件已創(chuàng)建");
System.out.println("文件信息:");
System.out.println("名字:"+file.getName());
System.out.println("長(zhǎng)度:"+file.length());
System.out.println("是否被隱藏:"+file.isHidden());
System.out.println("是否可讀:"+file.canRead());
System.out.println("是否可寫(xiě):"+file.canWrite());
System.out.println("絕對(duì)路徑:"+file.getAbsolutePath());
System.out.println("父類路徑:"+file.getParent());
System.out.println("最后修改時(shí)間:"+file.lastModified());
}
}
}
FileInputStream/FileOutputStream
package z2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileIoStream {
public static void main(String[] args) throws Exception{
File file=new File("d:/","love.txt");
//寫(xiě)入
FileOutputStream out=new FileOutputStream(file);
byte byin[]="人生若只如初見(jiàn)".getBytes();
out.write(byin);
out.close();
//getBytes() 是Java編程語(yǔ)言中將一個(gè)字符串轉(zhuǎn)化為一個(gè)字節(jié)數(shù)組byte[]的方法。
//String的getBytes()方法是得到一個(gè)系統(tǒng)默認(rèn)的編碼格式的字節(jié)數(shù)組铃在。
//讀取
FileInputStream in=new FileInputStream(file);
byte byin2[]=new byte[1024];
int len=in.read(byin);//獲取長(zhǎng)度
System.out.println(new String(byin,0,len));//取byin字節(jié)數(shù)組0-len的字節(jié)
}
}
FileWriter/FileReader
package z2;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class FileWriterReader {
public static void main(String[] args) throws Exception{
File file=new File("d:/","love.txt");
//寫(xiě)入
FileWriter out=new FileWriter(file);
out.write("111");
out.close();
//讀取
FileReader in=new FileReader(file);
char byin[]=new char[1024];
int len=in.read(byin);//獲取長(zhǎng)度
System.out.println(new String(byin,0,len));//取byin字節(jié)數(shù)組0-len的字節(jié)
}
}
BufferedWriter/BufferedReader
package z3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class BufferedIO {
public static void main (String[] args) throws Exception{
String content[]= {"我好想你","最近怎么樣","常聯(lián)系"};
File file=new File("d:/","BufferedWriteReader.txt");
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int i=0;i<content.length;i++) {
bw.write(content[i]);
bw.newLine();
}
bw.close();
fw.close();
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String s=null;
int i=0;
while((s=br.readLine())!=null) {
i++;
System.out.println("第"+i+"行:"+s);
}
fr.close();
br.close();
}
}
DataOutputStream/DataInputStream
package z4;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class DataIOStream {
public static void main(String[] args) throws Exception {
FileOutputStream fs = new FileOutputStream("word.txt");
DataOutputStream ds = new DataOutputStream(fs);
ds.writeUTF("使用writeUFT()方法寫(xiě)入數(shù)據(jù);");
ds.writeChars("使用writeChars()方法寫(xiě)入數(shù)據(jù);");
ds.writeBytes("使用writeBytes()方法寫(xiě)入數(shù)據(jù).");![![iostream2.png](https://upload-images.jianshu.io/upload_images/4448212-6d16ff4ad9f3063d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/4448212-edf8183217526309.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
ds.close();
FileInputStream fis = new FileInputStream("word.txt");
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readUTF());
}
}
總結(jié):
IO知識(shí)結(jié)構(gòu)
File類
File file=new File("d:/","love.txt");
file.createNewFile();
file.delete();
file.exists();//是否存在
file.getName();
file.length();
file.isHidden();
file.canRead();
file.canWrite();
file.getAbsolutePath();
file.getParent();
file.lastModified();//最后修改時(shí)間
InputStream(字節(jié)輸入父類)
read();
read(byte[] b);//讀取字節(jié)遥诉,以整形返回字節(jié)數(shù)
mark(int readlimit);//在輸入流當(dāng)前位置放置一個(gè)標(biāo)記,
//readlimit為在此標(biāo)記失效前允許讀取的字節(jié)數(shù)
reset();//將輸入指針?lè)祷氐疆?dāng)前所作標(biāo)記處
skip(long n);/跳過(guò)輸入流中的n個(gè)字節(jié)
markSupported();//如果當(dāng)前流支持mark()/reset()方法返回true
close();
OutputStream(字節(jié)輸出父類)
write(int b);
write(byte[] b);
write(byte[] b , int off , int len);
flush();//徹底完成輸出并清空緩存區(qū)
close();
BufferedWriter/BufferedReader
read();
readLine();//讀取一個(gè)文本行铐料,返回字符串
write(String s , int off , int len);
newLine();//寫(xiě)入一個(gè)行分隔符
flush();//刷新該流緩存
close();