路径操作方法:
new File()
exits():boolean 路径是否存在
mkdirs() 创建目录
createNewFile() 创建文件
isFile() 是否是文件
isDirectory() 是否是文件夹
list() 获取当前路径下的所有子路径名
流的操作方法:
FileInputStream
-read(byte[]) --输入流直接写入内存地址
-read() --循环读取字节,每次读取一个
---
-write(byte[]) --输出流直接输出到指定外部,赋值变量
-write() --循环写字节,每次写一个
(解码,不处理缓存)
Reader---InputStreamReader---FileReader
---BufferedReader
(处理缓冲,是将字节都进来,放进数组里,一次性返回)
(编码,不处理缓冲)
Writer---OutPutStreamWriter---FileWriter
---BufferedWriter
(1) 有缓冲
(2) Writer是没有真正的写出去,只是往数组里,添加元素
(3) Flush被调用的时候,才真正写出去-----------------------------------------桥梁作用:InputStreamReader:可以用来指定字符集-----------------------------------------
[由FilterInputSteam的read()]
InputStream---FileInputStream ---DataInputStream
通过本地调用(来获取字节)
OutputStream---FileOutPutStream---DataOutputStream
通过本地调用(没有重写flush)ArrayOutputStream --- ObjectOutputStreamArrayInputStream --- ObjectInputStream
简单采用集合解决读取问题:
//FileInputStream --直接读取字节
File file = new File("C:\D-drive-30078\斌斌的故事\斌斌的前三十年\第三年"
,"《领导力培养》.txt");
Long length = file.length();//读文件的大小(字节)
System.out.println("文件大小:"+length);
//FileInputStream————直接由数据流传递
InputStream input = new FileInputStream(file);
int len2 = input.available();//获取可读数据的大小(字节)
System.out.println("流中文件大小:"+len2);//也是用内部数组存储
//数组的创建
byte[] bs = new byte[len2];
input.read(bs);//输入流将直接写内存地址
String str = new String(bs);
System.out.println(str);
input.close();//或者每次只读取一个字节
byte temp = -1;
//2.如何把数据写入数据
int index = 0;
while ((temp=(byte)input.read())!=-1){
bs[index] = temp;
index++;
}//输出流File file = new File("C:\D-drive-30078\斌斌的故事\斌斌的前三十年\第三年"
,"《领导力培养》.txt");
//输出流:覆盖原文件里的内容,不是追加
String str = "第一章 hello,IOStream!";
OutputStream output = new FileOutputStream(file);
output.write(str.getBytes());
output.flush();//把缓冲器的数据推送到输出流里{当缓冲区数据量很大才需要调用}
output.close();//每次输出一个字节
str = "第二章 hello world";
byte[] bytes = str.getBytes();
for (byte b:bytes){
output.write(b);
}
output.close();
FileInputStream fInputStream = new FileInputStream(new File("test.txt"));
int temp = -1;
LinkedList<Character> list = new LinkedList<Character>();
while ((temp = fInputStream.read())!=-1) {
char c = (char)temp;
list.add(c);
}
Character[] chars = list.toArray(new Character[0]);
System.out.println(Arrays.toString(chars));
char[] chars2 = new char[chars.length];
//包装数组无法直接拆箱
//String.valueOf 将数组作为引用对象来处理
int index = 0;
for (Character character : chars) {
chars2[index++] = character;
}
String string = String.valueOf(chars2);
System.out.println(string);//不如StringBuffer更方便
StringBuffer sb = new StringBuffer();
sb.append(c);
//输入流
Reader reader = new InputStreamReader(
new FileInputStream(
new File("src/test/java/杨老师/IO_Stream"
,"READ.txt")));int c = -1;
while ((c = reader.read()) != -1) {
sb.append((char) c);
}
System.out.println(sb.toString());
reader.close();//输出流Writer write = new OutputStreamWriter(
new FileOutputStream(
new File("src/test/java/杨老师/IO_Stream"
,"READ.txt")
)
);
write.write("hello world! OutputStreamWriter");
write.flush();
write.close();
FileReader && FileWrite
Reader reader = new FileReader(
new File("src/test/java/杨老师/IO_Stream"
,"READ.txt"));LinkedList<Character> linkedList = new LinkedList<>();//使用泛型,强制统一数据类型
int c = (char)-1;
while((c= reader.read())!=-1){
linkedList.add((char)c);
}
reader.close();//FileWriterWriter write = new FileWriter(
new File("src/test/java/杨老师/IO_Stream"
,"READ.txt")
);
write.write("hello world!"+" "+"FileWriter");
write.flush();
write.close();
写入流的flush()作用汇总:建议无论是否起作用都加上!!(代填的坑)
FileOutputStream.flush(); //把缓冲器的数据推送到输出流里{当缓冲区数据量很大才需要调用}FileWriter 本质上最终调用的是FileOutputStream.flush()BufferedWrite.flush(); //必须写完之后,马上调用,将其写入数组中+?涉及到字符流的写入,最好都加上: flush()方法。
-InputStreamReader & OutputStreamWriter 不需要,只是编解码过程。
常用:写入流,切记flush();
//BufferedReader 构造应用
//方法 1
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File("src/test/java/杨老师/IO_Stream"
,"READ.txt"))));// 方法2
BufferedReader br1 = new BufferedReader(new FileReader(
new File(
"src/test/java/杨老师/IO_Stream"
,"READ.txt"
)
));//字符流,判断是否为nullStringBuffer sb = new StringBuffer();
String s="";
while((s = br.readLine())!=null){
sb.append(s);
sb.append(" ");
}
System.out.println(sb.toString());
br.close();//将数组内容写入flush()
String string = "hello world ,grow up";
try {
bw.write(string);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
DataInputStreamDataOutputStream
//实现将对象持久化File f = new File("src/test/java/杨老师/IO_Stream","Test.xz");
if(!f.exists()){
f.createNewFile();
}
//不是序列化
DataOutputStream out = new DataOutputStream(
new FileOutputStream(f)
);
out.writeInt(123);
out.writeUTF("ybb");
out.writeInt(3);
out.writeShort(34);
out.writeDouble(34.200);
out.flush();
out.close();
DataInputStream in = new DataInputStream(
new FileInputStream(f));
System.out.println(in.readInt());
System.out.println(in.readUTF());
System.out.println(in.readInt());
System.out.println(in.readShort());
System.out.println(in.readDouble());
in.close();
StringReader&StringWriter
//StringReader更方便操作数组中的” ”
//自己手写,采用StringBuffer也可以实现!!
String src = "从明天起,做一个幸福的人, 喂马,劈材,周游世界, 从明天起,关心粮食和蔬菜, "
+ "我有一所房子,面朝大海,春暖花开, 从明天起,和每一个人通信,告诉他们我的幸福 ";
StringReader sReader = new StringReader(src);
char[] buffer = new char[32];
int hasRead = 0;
try {
while ((hasRead=sReader.read(buffer))>0) {
System.out.println(new String(buffer,0,hasRead));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringWriter sWriter = new StringWriter();
sWriter.write("hello ");
sWriter.write("world");
System.out.println(sWriter.toString());
/*
底层估计差不多
*/
//不如用StringBuffer
StringBuffer sBuffer = new StringBuffer(src);
System.out.println(sBuffer);
//问题是处理换行怎么解决?
//System.out.println(sBuffer.indexOf(" "));
int start = 0;
int end = 0;
while ((end=sBuffer.indexOf(" ",start))>0) {
System.out.println(end+"~"+start);
System.out.println(sBuffer.substring(start, end));
start = end+1;
}
}
应用场景
字节流跟字符流使用
架构+中间件
完成业务——SpringMVC、SSH、MyBatis
字节流处理二进制:视频、图片
demo--理解;场景——应用
获取路径:
getCanonicalPath()返回的就是标准的将符号完全解析的路径
getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减
getAbsolutePath()返回的其实是user.dir+getPath()的内容;——后果:对于相对路径,会有问题。将 . , .. 这两种包含进去。
深复制:
ByteArrayOutputStream
ObjectOutputStream
--------------------------------
ByteArrayInputStream
ObjectInputStream
反射这块
//还是
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);