文件
相关概念:输入流、输出流
创建文件:
//方式1:路径创建
String pathName = "d:\\new1.txt";
File file = new File(pathName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//方式2:父目录文件+子路径
File fileParent = new File("d:\\");
String name = new String("news2.txt");
File file = new File(fileParent, name);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//方式3:父目录+子路径
String pathway = new String("d:\\");
String fileName = new String("new3.txt");
File file = new File(pathway, fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
常用文件操作:
File file = new File("d:\\hello.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件名:" + file.getName());
System.out.println("文件的绝对路径:" + file.getAbsoluteFile());
System.out.println("文件的父类目录" + file.getParent());
System.out.println("文件大小(byte):" + file.length());
System.out.println("文件是否存在:" + file.exists());
System.out.println("是否为文件:" + file.isFile());
System.out.println("是否为目录:" + file.isDirectory());
mkdir:创建一级目录
mkdirs:创建多级目录
delete():删除空目录或文件
String dir1 = new String("d:\\a");
File file1 = new File(dir1);
file1.mkdir();
String dir2 = new String("d:\\abc\\dd\\kim");
File file2 = new File(dir2);
file2.mkdirs();
file1.delete();
System.out.println(file2.delete());
IO流
分类:
(抽象基类) | 字节流 | 字符流 |
---|---|---|
输出流 | InputStream | Reader |
输入流 | OutputStream | Writer |
还可分为:节点流,包装流
IO流类图
字节流
字节输入流
FileInputStream
与FileOutputStream一起,常用于图片/音乐的拷贝
1.一个字节一个字节的读取,如果读到文件末尾,返回-1
String filename = new String("d:\\hello.txt");
FileInputStream stream = null;
try {
stream = new FileInputStream(filename);
int read;
//read方法:读取单个字节,返回该字节的码值,如果读到文件末尾,返回-1
while((read = stream.read()) != -1) {
System.out.print((char)read);
}
} catch (IOException e) {
e.printStackTrace();
}
stream.close();
2.读取多个字节到byte[],返回读取到的字节数,如果到达文件末尾,返回-1
String filename = new String("d:\\hello.txt");
FileInputStream stream = null;
byte b[] = new byte[20];
int length = 0;
try {
stream = new FileInputStream(filename);
while((length = stream.read(b)) != -1) {
System.out.print(new String(b, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
stream.close();
ObjectInputStream
反序列化,恢复被序列化的数据的值和数据类型
String pathway = "d:\\data.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new String(pathway)));
System.out.println(ois.readInt());
System.out.println(ois.readDouble());
System.out.println(ois.readBoolean());
System.out.println(ois.readUTF());
System.out.println(ois.readChar());
System.out.println(ois.readObject());
ois.close();
字节输出流
FileOutputStream
String pathway = new String("d:\\a.txt");
FileOutputStream stream = null;
String s = "Welcome To New York";
try {
//true,则表示为 追加模式,如果不写,默认为覆盖模式
stream = new FileOutputStream(pathway,true);
stream.write('a');
//getBytes表示返回字符串对应的字符数组
stream.write(s.getBytes());
stream.write(s.getBytes(),0,7);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
stream.close();
这里若按字节读取汉字,有可能造成乱码。
ObjectOutputStream
序列化,保存数据的值和数据类型
想要某对象能够实现序列化,其类也必须实现序列化,通常实现Serializable接口。
String pathway = "d:\\data.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new String(pathway)));
/*注意:
int-->Integer
double-->Double
boolean-->Boolean
char-->Char
String 都实现了Serializable接口
*/
oos.writeInt(100);
oos.writeDouble(1005.2);
oos.writeBoolean(false);
oos.writeUTF("美美的睡觉");
oos.writeChar('K');
//Goat对象实现了Serializable接口
oos.writeObject(new Goat("咩咩",11));
oos.close();
处理流
BufferedInputStream & BufferedOutputStream
读取到文件末尾时返回-1
关闭时,关闭外层流
完成某文件(音乐/图片的拷贝)
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte b[] = new byte[1024];
int len;
try {
bis = new BufferedInputStream(new FileInputStream(new String("d:\\beauty.jpg" )));
bos = new BufferedOutputStream(new FileOutputStream(new String("d:\\bb1.jpg")));
while((len = bis.read(b)) != -1){
bos.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
标准输入和输出
编译类型 | 运行类型 | 默认设备 | |
---|---|---|---|
标准输入 (System.in) | InputStream | BufferedInputStream | 键盘 |
标准输出(System.out) | PrintStream | PrintStream | 显示器 |
打印流(仅为输出流)
字节输出流----PrintStream
1.标准输出,即输出到显示器上
PrintStream ps = System.out;
ps.print("早上好,中午好,下午好");
ps.close();
2.输出到文件中
PrintStream ps = new PrintStream("d:\\a2123.txt");
System.setOut(ps);
System.out.println("你睡得着吗,我睡不着哈哈");
字符输出流----PrintWriter
//输出到某一文件
PrintWriter printWriter = new PrintWriter("d:\\a1.txt");
//输出到控制台上
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.write("You should bet on me.");
printWriter.close();
字符流
转换流
InputStreamReader : Reader 的子类,可以将InputStream 包装为 Reader
OutputStreamWriter : Writer 的子类,可以将 OutputStream 包装为 Writer
可以有效解决中文乱码问题,建议将字节流转换为字符流
可使用制定格式编码
InputStreamReader
FileImputStream(字节流)–>InputStreamReader–>BufferedReader(字符流)
带上字符编码!!
InputStreamReader isr = new InputStreamReader(new FileInputStream(new String("d:\\a.txt")),"gbk");
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
OutputStreamReader
指定路径和编码后,可直接卸如
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new String("d:\\a2.txt")), "gbk");
osw.write("你好鸭");
osw.close();
字符输入流
FileReader
第一种方式:按一个字符一个字符的读取,读取到文件末尾时返回-1:
fileReader.read();
第二种方式:批量读书多个字符到char buf[]数组,返回实际读取的个数,如果读到文件末尾,返回-1
String pathway = new String("d:\\story.txt");
FileReader fileReader = null;
char buf[] = new char[1024];
int len;
try {
fileReader = new FileReader(pathway);
while((len = fileReader.read(buf)) != -1){
//一次读1024个字符,读多次
System.out.println(new String(buf,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
fileReader.close();
其他方法:
new String(char[]):将char[]转化为String
new String(char[],off,len):将char[]的指定部分转化为String
字符输出流
FileWriter
String pathway = new String("d:\\note.txt");
FileWriter fileWriter = null;
char c[] = {'m','i','k','o'};
try {
fileWriter = new FileWriter(pathway);//true,表示追加
fileWriter.write('m');//写入字符
fileWriter.write(" 风雨之后,定见彩虹");//写入字符串
fileWriter.write(" 风雨之后",0,3);
fileWriter.write(c);
fileWriter.write(c,0,1);
fileWriter.write(c,0,1);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.close();//刷新+关闭
//fileWriter+flush();
} catch (IOException e) {
e.printStackTrace();
}
}
处理流
BufferedReader
readLine方法表示按行读取,返回读取的内容(String),当返回"null"时,读取完毕。
关闭时,只需关闭外层流
BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"));
String str = "";
while((str =br.readLine())!= null){
System.out.println(str);
}
br.close();
BufferedWriter
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\a123.txt"));
bw.write("hello,miko1");
bw.newLine();//用于换行
bw.write("hello,miko2");
bw.newLine();
bw.write("hello,miko3");
bw.newLine();
bw.close();