//IO流
package sunday;
/*
//file类
//字节流
//字符流
//特殊操作流
//获取文件绝对路径
//基本字节流一次读写一个字节
//基本字节流一次读写一个字节数组
//字节缓冲流一次读写一个字节
//字节缓冲流一次读写一个字节数组
//集合到文件排序版
//复制多级文件夹
//打印流复制文本文件
记录游戏次数
/
import java.io.;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Demo {
public static void main(String[] args) throws IOException {
//file();
//bytestream();
//charsteam();
specialOperationSteam();
//getAllPath();
//method();
//method1();
//method2();
//method3();
//copyFoldershow();
//treeSetToFile();
//printCopy();
//properties();
}//main
//file类
public static void
file ()
throws IOException {
File file=new File(“E:\idea”);
//getAllPath(file);
//方法
File file1=new File(“E:\idea\demo.txt”);
File file2=new File(“E:\idea\demo”);
File file3=new File(“E:\idea\demo\more”);
//创建文件,目录,多级目录,删除目标
file1.createNewFile();
file2.mkdir();
file3.mkdirs();
file1.delete();
//判断是否是目录、文件、存在
file.isDirectory();
file.isFile();
file.exists();
//返回绝对路径、相对路径、目标名称、字符串数组、file数组
file.getAbsolutePath();
file.getPath();
file.getName();
String[] list = file.list();
File[] files = file.listFiles();
}
//字节流
public static void
bytestream()
throws IOException {
FileOutputStream fileOutputStream=new FileOutputStream(“E:\idea\12.txt”,true);
FileInputStream fileInputStream=new FileInputStream(“E:\idea\12.txt”);
byte[] bytes=“wef”.getBytes();
//写入、返回字符串对应的字节数组、返回指定字节、释放资源、异常处理、读数据、遍历数据
fileOutputStream.write(97);
fileOutputStream.write(bytes);
fileOutputStream.write(bytes,1,bytes.length);
//fileOutputStream.close();
FileOutputStream fileOutputStream1=null;
try{
fileOutputStream1 =new FileOutputStream(“E:\idea\12.txt”,true);
fileOutputStream.write(“异常”.getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileOutputStream1!=null){
try{
fileOutputStream1.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
fileInputStream.read();
int by;
while ((by=fileInputStream.read())!=-1){
System.out.println(by);
}
}
//字符流
public static void
charsteam ()
throws IOException {
//简化方式,转换方式
FileReader fileReader=new FileReader(“E:\idea\demo\12.txt”);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(“E:\idea\demo\mark.txt”));
BufferedReader bufferedReader=new BufferedReader(new FileReader(“E:\idea\demo\12.txt”));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(“E:\idea\demo\mark.txt”));
//写入字符串,字符缓冲流读一行文字,写一行行分隔符,异常处理
outputStreamWriter.write(“robin”);
bufferedReader.readLine();
bufferedWriter.newLine();
try {
FileReader fileReader1 = new FileReader(“E:\idea\demo\12.txt”);
FileWriter fileWriter = new FileWriter(“12.txt”);
char[] chars = new char[1024];
int len;
while ((len = fileReader1.read()) != -1) {
fileWriter.write(chars, 0, len);
}
}catch (IOException e){
e.printStackTrace();
}
bufferedReader.close();
bufferedWriter.close();
outputStreamWriter.close();
}
//特殊操作流
public static void
specialOperationSteam()
throws IOException {
PrintWriter printWriter = new PrintWriter(new FileWriter(“E:\idea\test\12.txt”),true);
Properties properties = new Properties();
FileWriter fileWriter = new FileWriter(“E:\idea\July\12.txt”);
FileReader fileReader = new FileReader(“E:\idea\July\12.txt”);
//Properties集合添加元素,设置string类型键和值,setProperty对应返回方法,返回不可修改建集,特有遍历
//properties.put(1,“robin”);
properties.setProperty(“1”,“robin”);
properties.getProperty(“1”);
Set strings = properties.stringPropertyNames();
for(String key:strings){
String value = properties.getProperty(key);
System.out.println(key+value);
}
//把集合数据保存到文件中,把文件数据加载到集合
properties.store(fileWriter,null);
properties.load(fileReader);
fileWriter.close();
}
//获取文件绝对路径
public static void
getAllPath(File file)
{
File[] farr=file.listFiles();
if(farr!=null){
for(File file1:farr){
if(file1.isDirectory()){
getAllPath(file1);
}else{
System.out.println(file1.getAbsolutePath());
}
}
}
}
//基本字节流一次读写一个字节
public static void
method()
throws IOException {
FileInputStream fileInputStream=new FileInputStream(“D:\qq\2765897147\FileRecv\jb.mp4”);
FileOutputStream fileOutputStream=new FileOutputStream(“E:\idea\jb.mp4”);
int by;
while ((by=fileInputStream.read())!=-1){
fileOutputStream.write(by);
}
fileInputStream.close();
fileOutputStream.close();
}
//基本字节流一次读写一个字节数组
public static void
method1()
throws IOException {
FileInputStream fileInputStream=new FileInputStream(“D:\qq\2765897147\FileRecv\jb.mp4”);
FileOutputStream fileOutputStream=new FileOutputStream(“E:\idea\jb.mp4”);
byte[] bytes=new byte[1024];
int len;
while ((len=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
//字节缓冲流一次读写一个字节
public static void
method2()
throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(“D:\qq\2765897147\FileRecv\jb.mp4”));
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(“E:\idea\jb.mp4”));
int by;
while ((by=bufferedInputStream.read())!=-1){
bufferedOutputStream.write(by);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
//字节缓冲流一次读写一个字节数组
public static void
method3()
throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(“E:\idea\12.txt”));
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(“E:\idea\demo\12.txt”));
byte[] bytes=new byte[1024];
int len;
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
//集合到文件排序版
public static void
treeSetToFile()
throws IOException {
TreeSet treeSet = new TreeSet<>(new Comparator() {
@Override//排序
public int compare(Tools o1, Tools o2) {
int num = o2.getsum() - o1.getsum();
int num1 = num == 0 ? o1.getChinese() - o2.getChinese() : num;
int num2 = num1 == 0 ? o1.getMath() - o2.getMath() : num1;
int num3 = num2 == 0 ? o1.getEnglish() - o2.getEnglish() : num2;
return num3;
}
});
//录入数据;
for(int i=0;i<3;i++){
Scanner scanner = new Scanner(System.in);
System.out.println(“第”+i+“个”);
System.out.println(“姓名”);
String name=scanner.nextLine();
System.out.println(“语文成绩”);
int chinese = scanner.nextInt();
System.out.println(“数学成绩”);
int math = scanner.nextInt();
System.out.println(“英语成绩”);
int english = scanner.nextInt();
Tools tools = new Tools();
tools.setName(name);
tools.setChinese(chinese);
tools.setMath(math);
tools.setEnglish(english);
treeSet.add(tools);
}
//创建字符缓冲输出对象;
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(“E:\idea\demo\mark.txt”));
//遍历集合得到对象;
for(Tools t:treeSet){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(t.getName()+" “).append(t.getChinese()+” ").append(t.getMath()).append(t.getEnglish()).append(t.getsum());
bufferedWriter.write(stringBuilder.toString());
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
//复制多级文件夹
public static void
copyFoldershow()
throws IOException {
File srcfile=new File(“E:\idea\demo”);
File destfile=new File(“E:\idea\test”);
copyFolder(srcfile,destfile);
}
private static void copyFolder(File srcfile,File destfile) throws IOException {
//判断数据源File是否在目录
if(srcfile.isDirectory()) {
//在目的地创建和数据源File名称一样的目录
String srcfileName = srcfile.getName();
File newfolder=new File(destfile,srcfileName);
if(!newfolder.exists()){
newfolder.mkdir();
}
//获取数据源File下所有目标的File数组
File[] fileArray=srcfile.listFiles();
//遍历该File数组,得到每一个File对象
for(File file:fileArray){
//把该File作为数据源File对象,递归调用复制文件夹的方法
copyFolder(file,newfolder);
}
}else {
//判定目标是文件,直接用字节流复制
File newfile=new File(destfile,srcfile.getName());
copyFile(srcfile,newfile);
}
}
private static void copyFile(File srcfile,File destfile) throws IOException {
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(srcfile));
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(destfile));
byte[] bytes=new byte[1024];
int len;
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
//打印流复制文本文件
public static void
printCopy()
throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(“E:\idea\12.txt”));
PrintWriter printWriter = new PrintWriter(new FileWriter(“E:\idea\test\12.txt”),true);
String line;
while ((line=bufferedReader.readLine())!=null){
printWriter.println(line);
}
bufferedReader.close();
printWriter.close();
}
//记录游戏次数
public static void
properties()
throws IOException {
//从文件中读取数据到properties集合,用load()方法实现
Properties properties = new Properties();
FileReader fileReader = new FileReader(“E:\idea\July\12.txt”);
properties.load(fileReader);
fileReader.close();
//通过properties集合获取到玩游戏的次数
String count = properties.getProperty(“count”);
int number = Integer.parseInt(count);
//判断次数是否到三次
if(number>=3) {
//如果到了,给出提示
System.out.println(“游戏试玩已结束,想玩请充值”);
}else {
//玩游戏
guessNumber();
//次数+1,重新写回文件,用store方法实现
number++;
properties.setProperty(“count”,String.valueOf(number));
FileWriter fileWriter = new FileWriter(“E:\idea\July\12.txt”);
properties.store(fileWriter,null);
fileWriter.close();
}
}
private static void guessNumber(){
Random random = new Random();
//int number = random.nextInt(10) + 1;
int number=1;
while (true){
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入你猜的数字”);
int guessnumber = scanner.nextInt();
if(guessnumber>number){
System.out.println(“大了”);
}else if(guessnumber<number){
System.out.println(“小了”);
}else if(guessnumber==number){
System.out.println(“对了”);
break;
}
}
}
}//end
//public
心得
/*
目录或文件重名会创建失败
文件可直接创建于项目目录
目录下有内容不可删除
递归要找出口与相似规则
文件末尾为-1
读数据结果为字节编号
字符流遇到编码问题要用转换方式
*/