Java 流概述
文件通常是由一连串的字节或字符构成,组成文件的字节序列称为字节流,组成文件的字符序列称为字符流。Java 中根据流的方向可以分为输入流和输出流。输入流是将文件或其它输入设备的数据加载到内存的过程;输出流恰恰相反,是将内存中的数据保存到文件或其他输出设 备
流:数据在数据源文件和程序内存直接经历的路径
输入流:数据从数据源文件到程序内存的路径
输出流:数据从程序内存到数据源文件的路径
IO流
操作文件Input
(读文件):把数据从物理内存加载到运行内存Output
(写文件):把数据从运行内存写出到物理内存
传输:本地传输、网络传输、对象传输
常用文件操作
创建文件方法
new File(String pathname)
:根据路径构建一个File对象new File(File parent,Sring child)
:根据父目录文件+子路径构建new File(String parent,String child)
:根据父目录+子路径构建
public class FileCreate {
public static void main(String[] args) {}
//方式1 new File(String pathname)
@Test
public void create01() {
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式2 new File(File parent,String child) //根据父目录文件+子路径构建
//e:\\news2.txt
@Test
public void create02() {
File parentFile = new File("e:\\");
String fileName = "news2.txt";
//这里的file对象,在java程序中,只是一个对象,创建在内存中
//只有执行了createNewFile 方法,才会真正的,在磁盘创建该文件
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式3 new File(String parent,String child) //根据父目录+子路径构建
@Test
public void create03() {
String parentPath = "e:\\";
String fileName = "news4.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}
获取文件相关信息
getName
文件名字getAbsolutePath
文件绝对路径getParent
文件父级目录length
文件大小(字节)exists
文件是否存在isFile
是不是一个文件isDirectory
是不是一个目录public class FileInformation { public static void main(String[] args) {} //获取文件的信息 @Test public void info() { //先创建文件对象 File file = new File("e:\\news1.txt"); //注意,这里没有用createNewFile方法,因为这并不是在创建文件,而是创建这个已经存在的文件的一个对象,来对这个文件进行一些操作 //调用相应的方法,得到对应信息 System.out.println("文件名字=" + file.getName()); //getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory System.out.println("文件绝对路径=" + file.getAbsolutePath()); System.out.println("文件父级目录=" + file.getParent()); System.out.println("文件大小(字节)=" + file.length()); System.out.println("文件是否存在=" + file.exists());//T System.out.println("是不是一个文件=" + file.isFile());//T System.out.println("是不是一个目录=" + file.isDirectory());//F } }
目录的操作和文件删除
mkdir
创建一级目录mkdirs
创建多级目录delete
删除空目录或文件
InputStream
InputStream
抽象类式所有类字节输入流的超类
FileInputStream
:文件输入流BufferedInputStream
:缓冲字节输入流ObjectlnputStream
:对象字节输入流
使用IO流对文本的操作示例
Goods类
package com.domcer.java20220810.morning;
public class Goods {
private Integer id;
private String name;
private Integer price;
public Goods() {
}
public Goods(Integer id, String name, Integer price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
Shop类
package com.domcer.java20220810.morning;
import com.domcer.java20220809.morning.util.IOUtil;
import java.io.*;
import java.util.*;
public class Shop {
private static List<Goods> list = new ArrayList<>(16);
private static BufferedReader reader = null;
private static BufferedWriter writer = null;
public static void insert(Scanner sc) {
System.out.println("请输入商品编号:");
int id = sc.nextInt();
System.out.println("请输入商品名称:");
String name = sc.next();
System.out.println("请输入商品价格:");
int price = sc.nextInt();
// 最简单,直接追加
try {
writer = new BufferedWriter(new FileWriter("e:/goods.txt",true));
writer.write(id + " " + name + " " + price);
writer.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeIO(null,writer);
}
}
public static void delete(Scanner sc) {
System.out.println("请输入商品编号:");
// 全部拿出来,删除后覆盖
int id = sc.nextInt();
try {
reader = new BufferedReader(new FileReader("e:/goods.txt"));
String str;
while((str = reader.readLine()) != null){
String[] s = str.split(" ");
Goods goods = new Goods(Integer.parseInt(s[0]),s[1],Integer.parseInt(s[2]));
list.add(goods);
}
// 输出流的实例化的位置!!!
writer = new BufferedWriter(new FileWriter("e:/goods.txt"));
Iterator<Goods> iterator = list.iterator();
while(iterator.hasNext()) {
Goods goods = iterator.next();
if(id == goods.getId()){
iterator.remove();
}
}
for (Goods goods : list) {
writer.write(goods.getId() + " " + goods.getName() + " " + goods.getPrice());
writer.newLine();
}
System.out.println("删除成功...");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeIO(reader,writer);
}
}
public static void update(Scanner sc) {
System.out.println("请输入商品编号:");
// 全部拿出来,更新后覆盖
int id = sc.nextInt();
try {
reader = new BufferedReader(new FileReader("e:/goods.txt"));
String str;
while((str = reader.readLine()) != null){
String[] s = str.split(" ");
Goods goods = new Goods(Integer.parseInt(s[0]),s[1],Integer.parseInt(s[2]));
list.add(goods);
}
for (Goods goods : list) {
if(id == goods.getId()) {
System.out.println("请输入商品新的名称:(原名称为:" + goods.getName() + ")");
String name = sc.next();
System.out.println("请输入商品新的价格:(原价格为:" + goods.getPrice() + ")");
int price = sc.nextInt();
goods.setName(name);
goods.setPrice(price);
}
}
writer = new BufferedWriter(new FileWriter("e:/goods.txt"));
for (Goods goods : list) {
writer.write(goods.getId() + " " + goods.getName() + " " + goods.getPrice());
writer.newLine();
}
System.out.println("修改成功...");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeIO(reader,writer);
}
}
public static void findOne(Scanner sc) {
System.out.println("请输入商品编号:");
int id = sc.nextInt();
// 思路一:一行一行的读,找到为止
try {
reader = new BufferedReader(new FileReader("e:/goods.txt"));
String str;
while((str = reader.readLine()) != null) {
String[] s = str.split(" ");
if(id == Integer.parseInt(s[0])){
System.out.println("编号:" + s[0] + ",名称:" + s[1] + ",价格:" + s[2]);
break;
}else {
System.out.println("没有编号是【" + id + "】的商品");
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtil.closeIO(reader,null);
}
// 思路二:全部读取到内存,内存里找
}
}
Main类
package com.domcer.java20220810.morning;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择功能:1、插入新商品 2、删除商品 3、修改商品 4、查找一个商品 5、退出");
String flag = scanner.next();
switch (flag) {
case "1":
Shop.insert(scanner);
break;
case "2":
Shop.delete(scanner);
break;
case "3":
Shop.update(scanner);
break;
case "4":
Shop.findOne(scanner);
break;
case "5":
System.out.println("系统退出...");
System.exit(-1);
}
}
}
有些人把梦想变成现实,有些人把现实变成了梦想。关键是,你的梦想是什么,你为你的梦想做了什么。有梦想,就不会寂寞。当你寂寞的时候,只要招招手,你的梦想就飞到了你身边。剩下的事,就是琢磨怎样把梦想变成行动了。 ——毕淑敏