2020-8-05JAVA学习

1.File

在这里插入图片描述
File可以作为文件的对象,也可以作为目录的对象

File file = new File("G:/text.txt");//绝对路径
		File file1 = new File("G:","text.txt");//父目录下的文件
		File file2 = new File("G:/sql");//文件对象
		File file3 = new File(file2,"text.txt");
		
		System.out.println(file.getName());
		System.out.println(file1.getName());
		System.out.println(file2.getName());
		System.out.println(file3.getName());

2.文件的创建和删除

创建文件

File file4 = new File("G:/AAA/text.txt");
boolean createNewFile = file4.createNewFile();
		System.out.println(createNewFile);

创建单级目录

File file5 = new File("G:/AAA/bb");
		boolean mkdir = file5.mkdir();
		System.out.println(mkdir);

创建多级目录

File file6 = new File("G:/AAA/cc/ee/ff");
		boolean mkdirs = file6.mkdirs();
		System.out.println(mkdirs);

删除

file.delete();//不走回收站

对目录的话,只有空目录才能被删除

递归删除文件:

在这里插入代码片

2.IO流

字符流
字符输入流超类:Reader
字符输出流超类:Write

输入:

public class WriteDemo {
	public static void main(String[] args) {
		File file = new File("G:/text.txt");
		System.out.println(file.exists());
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(file);
			fileWriter.write("hellow world");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			if(fileWriter != null){
				try {
					fileWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

工作过程中,写入的数据比较庞大,可以过一段时间清空一次缓冲区

for (int i = 0; i < 100; i++) {
				fileWriter.write("hellow world");
				if(i % 10 == 0){
					fileWriter.flush();
				}
			}

文件的追加

fileWriter = new FileWriter(file,true);

换行:
Window:\r\n
Linux:\n
Mac:\r

字符输入流

步骤

  • 创建字符输入流对象FileReader
  • 读取数据
  • 关闭输入流

public class ReaderDemo {
	public static void main(String[] args) {
		File file = new File("G:/text.txt");
		FileReader fileReader = null;
		FileReader fileReader2 = null;
		try {
			fileReader = new FileReader(file);
			fileReader2 = new FileReader(file);
			char[] a = new char[5];
			int len = fileReader.read(a);
			while (len != -1) {
				String string = new String(a, 0, len);
				System.out.println(string);
				len = fileReader.read(a);
			}
			
			int  flag = fileReader2.read();
			String str = "";
			while(flag != -1) { //读单个字符
				char c = (char)flag;
				str += String.valueOf(c);
				flag = fileReader2.read();
			}
			System.out.println(str);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fileReader != null){
				try {
					fileReader.close();
					fileReader2.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

文件复制:

package learn;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyDemo {
	public static void main(String[] args) {
		File file = new File("G:/text.txt");
		File file1 = new File("G:/Copytext.txt");
		if (!file1.exists()) {
			try {
				file1.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
		FileReader reader = null;
		FileWriter writer = null;
		try {
			reader = new FileReader(file);
			writer = new FileWriter(file1);
			char[] cbuf = new char[1024];
			try {
				int len = reader.read(cbuf);
				while (len != -1) {
					writer.write(cbuf);
					len = reader.read(cbuf);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally{
			try {
				writer.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
	}
}

高效缓存输入流

package learn;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.Buffer;

public class BufferReaderDemo {
	public static void main(String[] args) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("src/learn/CollectionDemo.java"));
			try {
				String readLine = reader.readLine();//边界是null
				while(readLine != null){
					System.out.println(readLine);
					readLine = reader.readLine();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

高效缓存输出流,文件复制

package learn;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferWriteDemo {
	public static void main(String[] args) {
		BufferedWriter writer = null;
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("src/learn/List_Demo.java"));
			writer = new BufferedWriter(new FileWriter("G:/text.txt"));
			String readLine = reader.readLine();
			while (readLine != null) {
				writer.write(readLine);
				writer.newLine();
				readLine = reader.readLine();
				writer.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try{
				if (writer != null) {
					writer.close();
				}
				if (reader != null) {
					reader.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}
	}
}

字节流

字节流不需要flush
FileOutputStream
FileInputStream

package learn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IoByteCopy {
	public static void main(String[] args) {
		FileOutputStream out = null;
		FileInputStream in = null;
		
		try {
			out = new FileOutputStream("G:/timg.jpg");
			in = new FileInputStream(new File("G:/Copytimg.jpg"));
			
			
			byte[] a = new byte[1024];
			int len = in.read(a);
			while(len != -1){
				out.write(a, 0, len);
				len = in.read(a);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
			
		}
	}
}

高效缓存字节流BufferedInputStream BufferedOutputStream

package learn;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BufferIoDemo {
	public static void main(String[] args) {
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("G:/text.txt"));
			out = new BufferedOutputStream(new FileOutputStream("G:/Copytext.txt"));
			
			byte[] b = new byte[1024];
			int read = in.read(b);
			while(read != -1){
				out.write(b);
				read = in.read();
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
	}
}

字符流和字节流的转换桥梁

在这里插入代码片

3.打印流

package learn;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintDemo {
	public static void main(String[] args) {
		PrintWriter printWriter = null;
		
		try {
			printWriter = new PrintWriter("G:/text.txt");
			printWriter.print("1234");
			printWriter.print("asdqwe");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(printWriter != null){
				printWriter.close();
			}
		}
	}
}

4.属性类Properties

特点:

  1. 继承与Hashtable,是线程安全的键值对存储结构
  2. Properties可保存与流或在流中加载
  3. 只能保存字符串的键值对

打印到流中:

package learn;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

public class ProTest {
	public static void main(String[] args) {
		Properties properties = new Properties();
		//存储键值对
		properties.setProperty("001", "wyx");
		properties.setProperty("002", "zyy");
		
		PrintWriter printWriter = null;
		
		try { //输出到文件中
			printWriter = new PrintWriter("G:/text.txt");
			properties.list(printWriter);
			printWriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(printWriter != null){
				printWriter.close();
			}
		}
	}
}

读取:

package learn;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

public class ProTest {
	public static void main(String[] args) {
		Properties properties = new Properties();
		
		FileInputStream in = null;
		
		try {
			in = new FileInputStream("G:/text.txt");
			properties.load(in);
			System.out.println(properties);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

4.对象流

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值