如果当初陪着你的人走远了,那些日子回忆起来会模糊吗?
mark 标记
reset 重置
exist 存在
modified 变更
skip 跳过
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("文件已创建");
System.out.println("文件信息:");
System.out.println("名字:"+file.getName());
System.out.println("长度:"+file.length());
System.out.println("是否被隐藏:"+file.isHidden());
System.out.println("是否可读:"+file.canRead());
System.out.println("是否可写:"+file.canWrite());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("父类路径:"+file.getParent());
System.out.println("最后修改时间:"+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");
//写入
FileOutputStream out=new FileOutputStream(file);
byte byin[]="人生若只如初见".getBytes();
out.write(byin);
out.close();
//getBytes() 是Java编程语言中将一个字符串转化为一个字节数组byte[]的方法。
//String的getBytes()方法是得到一个系统默认的编码格式的字节数组。
//读取
FileInputStream in=new FileInputStream(file);
byte byin2[]=new byte[1024];
int len=in.read(byin);//获取长度
System.out.println(new String(byin,0,len));//取byin字节数组0-len的字节
}
}
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");
//写入
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);//获取长度
System.out.println(new String(byin,0,len));//取byin字节数组0-len的字节
}
}
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[]= {"我好想你","最近怎么样","常联系"};
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()方法写入数据;");
ds.writeChars("使用writeChars()方法写入数据;");
ds.writeBytes("使用writeBytes()方法写入数据.");![![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());
}
}
总结:
IO知识结构
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();//最后修改时间
InputStream(字节输入父类)
read();
read(byte[] b);//读取字节,以整形返回字节数
mark(int readlimit);//在输入流当前位置放置一个标记,
//readlimit为在此标记失效前允许读取的字节数
reset();//将输入指针返回到当前所作标记处
skip(long n);/跳过输入流中的n个字节
markSupported();//如果当前流支持mark()/reset()方法返回true
close();
OutputStream(字节输出父类)
write(int b);
write(byte[] b);
write(byte[] b , int off , int len);
flush();//彻底完成输出并清空缓存区
close();
BufferedWriter/BufferedReader
read();
readLine();//读取一个文本行,返回字符串
write(String s , int off , int len);
newLine();//写入一个行分隔符
flush();//刷新该流缓存
close();