// TODO Java 数据 + 流(转)操作
// 数据从哪里来,到哪里去
// TODO IO
// I : Input , 输入(In)
// O : Output , 输出(Out)
// Stream : 流转
public static void main(String[] args) throws Exception {
// TODO Java IO - 文件流
// TODO File : 文件类型(文件,文件夹),属于Java.io
// 创建文件对象 , 使用文件路径关联系统文件
String filepath = "C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data";
File file = new File(filepath);
System.out.println(file);
// 文件对象的操作
// // TODO 判断当前文件对象是否是文件
// System.out.println(file.isFile());
// // TODO 判断当前文件对象是否是文件夹
// System.out.println(file.isDirectory());
// // TODO 判断文件对象是否存在关联
// System.out.println(file.exists());
if ( file.exists() ) {
// TODO 文件对象存在的情况
System.out.println("文件对象存在,关联成功");
if (file.isFile()) {
System.out.println("文件对象关联的是一个文件");
System.out.println(file.getName());
System.out.println(file.length());
System.out.println(file.lastModified());
System.out.println(file.getAbsolutePath());
} else if ( file.isDirectory() ) {
System.out.println("文件对象关联的是一个文件夹");
System.out.println(file.getName());
System.out.println(new Date(file.lastModified()));
System.out.println(file.getAbsolutePath());
String[] list = file.list();
System.out.println("文件夹中的文件:");
for (String s : list) {
System.out.println(s);
}
System.out.println("文件夹中的文件对象:");
File[] files = file.listFiles();
for (File s1 : files) {
System.out.println(s1);
}
}
} else {
// TODO 文件对象不存在的情况
System.out.println("文件对象不存在,没有关联成功");
// TODO 创建多级文件目录
// file.mkdirs();
// TODO 创建新文件
// file.createNewFile();
}
}
// TODO Java IO - 文件复制
// TODO 数据源文件对象
File srcfile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成文件)
File destFile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt.copy");
// TODO 文件输入流(管道对象)
FileInputStream in = null;
// TODO 文件输出流(管道对象)
FileOutputStream out = null;
try {
in = new FileInputStream(srcfile);
out = new FileOutputStream(destFile);
// TODO 打开阀门,流转数据(输入)
int data = -1;
// TODO 打开阀门,流转数据(输出)
//out.write(data);
// TODO 问题 1 :代码重复性强
// TODO 问题 2 : 多读数据
// data = in.read();
// out.write(data);
//
// data = in.read();
// out.write(data);
//
// data = in.read();
// out.write(data);
//
// data = in.read();
// out.write(data);
//
// // TODO 如果文件数据已经全部读取完毕后,那么再去读数据,读取的结果就是-1,表示无效(结尾)
// data = in.read();
// if ( data != -1 ) {
// out.write(data);
// }
// //System.out.println(data);
while ( (data = in.read()) != -1) {
out.write(data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( in != null ) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( out != null ) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// TODO Java IO - 文件复制 - 缓冲
// TODO 数据源文件对象
File srcfile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成文件)
File destFile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt.copy");
// TODO 文件输入流(管道对象)
FileInputStream in = null;
// TODO 文件输出流(管道对象)
FileOutputStream out = null;
// TODO 缓冲输入流(管道对象)
BufferedInputStream bufferIn = null;
// TODO 缓冲输出流(管道对象)
BufferedOutputStream bufferOut = null;
// TODO 缓冲区(水桶)
byte[] cache = new byte[1024];
try {
in = new FileInputStream(srcfile);
out = new FileOutputStream(destFile);
bufferIn = new BufferedInputStream(in);
bufferOut = new BufferedOutputStream(out);
// TODO 打开阀门,流转数据(输入)
int data = -1;
while ( (data = bufferIn.read(cache)) != -1) {
bufferOut.write(cache,0,data);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( bufferIn != null ) {
try {
bufferIn.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( bufferOut != null ) {
try {
bufferOut.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// TODO Java IO - 文件复制 - 字符串
// TODO 数据源文件对象
File srcfile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成文件)
File destFile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt.copy");
// TODO 文件输入流(管道对象)
FileInputStream in = null;
// TODO 文件输出流(管道对象)
FileOutputStream out = null;
try {
in = new FileInputStream(srcfile);
out = new FileOutputStream(destFile);
// TODO 打开阀门,流转数据(输入)
int data = -1;
StringBuilder ss = new StringBuilder();
while ( (data = in.read()) != -1) {
//System.out.println(data);
//out.write(data + 1);
ss.append((char) data);
}
ss.append(" zhangsan ");
System.out.println(ss);
// 可以将字符串转换成字节数组,再将数组中的每一个字节写入到文件中
// Unicode 编码
// byte => -128 ~ 127
// asc => 0 ~ 127
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( in != null ) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( out != null ) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// TODO Java IO - 文件复制 - 字符串
// TODO 数据源文件对象
File srcfile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt");
// TODO 数据目的地文件对象(自动生成文件)
File destFile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\word.txt.copy");
// TODO 字符输入流(管道对象)
BufferedReader reader = null;
// TODO 字符输出流(管道对象)
PrintWriter writer = null;
try {
reader = new BufferedReader(new FileReader(srcfile));
writer = new PrintWriter(destFile);
// TODO 打开阀门,流转数据(输入)
// TODO 读取文件中一行数据(字符串)
String line = null;
while ( (line = reader.readLine()) != null) {
System.out.println(line);
writer.println(line);
}
// 刷写数据
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if ( reader != null ) {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if ( writer != null ) {
writer.close();
}
}
// TODO Java IO - 文件复制 - 序列化 & 反序列化
// TODO 数据文件对象
File dataFile = new File("C:\\Users\\vvvv\\IdeaProjects\\java-top-speed\\data\\obj.data");
// TODO 对象输出流(管道对象)
ObjectOutputStream objectOut = null;
FileOutputStream out = null;
// TODO 对象输入流
ObjectInputStream objectIn = null;
FileInputStream in = null;
try {
// out = new FileOutputStream(dataFile);
// objectOut = new ObjectOutputStream(out);
// Java 中只有增加了特殊标记的类,才能在写文件时进行序列化操作
// 这里的标记其实就是接口
// User user = new User();
// objectOut.writeObject(user);
// objectOut.flush();
// TODO 从文件中读取数据转换成对象
in = new FileInputStream(dataFile);
objectIn = new ObjectInputStream(in);
Object o = objectIn.readObject();
System.out.println(o);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if ( objectOut != null ) {
try {
objectOut.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
class User implements Serializable {
}
// FileNotFoundException , IOException
// FileInputStream in = null;
// // ClassNotFoundException , NotSerializableException
// ObjectInputStream objIn = null;
// ObjectOutputStream objOut = null;
//
// try {
// in = new FileInputStream("xxx");
//
// in.read();
// objOut.writeObject();
// objIn.readObject();
// } catch (Exception e) {
//
// } finally {
// if ( in != null ) {
// try {
// in.close();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// }
}