目录
/** * 处理流之一:缓冲流的使用 * BufferedInputStream * BufferedOutputStream * BufferedReader * BufferedWriter * 2、作用:提供流的读取,写入速度 * 提高读写速度的原因,内部提供了一个缓冲区 * * 3、处理流,就是套在已有的流的基础上 * */
- public class Bufferedtest {
-
- @Test
- public void BufferedInputStreamTest(){
- long stast=System.currentTimeMillis();
-
- //1、造文件
- File file = new File("花花.png");
- File file1 = new File("花花2.png");
-
- //2、造流
- //2.1造节点流
- FileInputStream fi=null;
- FileOutputStream fo=null;
- BufferedInputStream bd=null;
- BufferedOutputStream bo=null;
- try {
- fi=new FileInputStream(file);
- fo=new FileOutputStream(file1);
- //2.2造缓冲流
- bd=new BufferedInputStream(fi);
- bo=new BufferedOutputStream(fo);
-
- //3、赋值的细节:读取与写入
- byte[] bytes=new byte[5];
- int len;
- while ((len=bd.read(bytes))!=-1){
- bo.write(bytes,0,len);
- }
- System.out.println("完成");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- /*
- 内层流可以省略,因为关闭外层流的同时,内层流也会关闭
- */
- if(bo!=null){
- try {
- bo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(bd!=null){
- try {
- bd.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(fo!=null){
- try {
- fo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(fi!=null){
- try {
- fi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- long end=System.currentTimeMillis();
- System.out.println("方式一所用时间为:"+(end-stast));//42
- }
-
- public void copyFile(String str1,String str2){
- //1、造文件
- File file = new File(str1);
- File file1 = new File(str2);
-
- //2、造流
- //2.1造节点流
- FileInputStream fi=null;
- FileOutputStream fo=null;
- BufferedInputStream bd=null;
- BufferedOutputStream bo=null;
- try {
- fi=new FileInputStream(file);
- fo=new FileOutputStream(file1);
- //2.2造缓冲流
- bd=new BufferedInputStream(fi);
- bo=new BufferedOutputStream(fo);
-
- //3、赋值的细节:读取与写入
- byte[] bytes=new byte[5];
- int len;
- while ((len=bd.read(bytes))!=-1){
- bo.write(bytes,0,len);
- }
- System.out.println("完成");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- /*
- 内层流可以省略,因为关闭外层流的同时,内层流也会关闭
- */
- if(bo!=null){
- try {
- bo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(bd!=null){
- try {
- bd.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(fo!=null){
- try {
- fo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(fi!=null){
- try {
- fi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- @Test
- public void runFile(){
- long staet=System.currentTimeMillis();
- String str1="花花.png";
- String str2="花花3.png";
- copyFile(str1,str2);
- long end =System.currentTimeMillis();
- System.out.println("方式二所有时间为:"+(end-staet));//37
- }
-
-
- @Test
- public void BufferedReaderWriter(){
- BufferedReader bufferedReader =null;
- BufferedWriter bufferedWriter =null;
- try {
- bufferedReader = new BufferedReader(new FileReader(new File("hello.txt")));
- bufferedWriter = new BufferedWriter(new FileWriter(new File("hello3.txt")));
-
- //方式一
- char[] chars=new char[5];
- int len;
- while ((len=bufferedReader.read(chars))!=-1){
- bufferedWriter.write(chars,0,len);
- bufferedWriter.flush();
- }
-
- //方式二:
- // String str;
- // while ((str=bufferedReader.readLine())!=null){
- // bufferedWriter.write(str);
- // bufferedWriter.newLine();
- // }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if (bufferedReader!=null){
- try {
- bufferedReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bufferedWriter!=null){
- try {
- bufferedWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
/** * File类的使用: * * 1、File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹) * 2、File类声明在Java.io包下 * 3、File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法 * 并未涉及到写入或读取文件内容的操作,如果需要读取或写入文件内容,必须使用IO流来完成 * 4、后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的“终点” * */
- public class FileTest1 {
-
- //1、创建File类的实例
- //File(String filepath)
- //File(String parentPath,String childPath)
- //File(File parentFile,String childPath)
-
- //2、相对路径:
- // 绝对路径:包含目录
-
- @Test
- public void test(){
- //构造器1
- File file=new File("hello.txt");//相对路径
- File file1=new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");//绝对路径
-
-
- System.out.println(file);
- System.out.println(file1);
- //构造方法
-
- //构造器2
- File file2=new File("D:\\WenJian\\IDEA\\Java高级基础部分","Day08输入输出流");
- System.out.println(file2);
-
- //构造器3
- File file3 = new File(file2, "hello.txt");
- System.out.println(file3);
- }
-
- @Test
- public void test1(){
- /*
- public String getAbsolutePath():获取绝对路径
- public String getPath() :获取路径
- public String getName() :获取名称
- public String getParent():获取上层文件目录路径。若无,返回null
- public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
- public long lastModified() :获取最后一次的修改时间,毫秒值
- public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
- public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
- */
-
-
- File file=new File("hello.txt");
- File file1 = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\hello.txt");
-
- System.out.println(file.getAbsolutePath());
- System.out.println(file.getPath());
- System.out.println(file.getName());
- System.out.println(file.getParent());
- System.out.println(file.length());
- System.out.println(file.lastModified());
- System.out.println(file.list());
- System.out.println(file.listFiles());
-
- System.out.println();
- //file1
- System.out.println(file1.getAbsolutePath());
- System.out.println(file1.getPath());
- System.out.println(file1.getName());
- System.out.println(file1.getParent());
- System.out.println(file1.length());
- System.out.println(file1.lastModified());
- System.out.println(file1.list());
- System.out.println(file1.listFiles());
- }
- @Test
- public void test2(){
- File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分");
- String[] lists=file.list();
- for (String s: lists) {
- System.out.println(s);
- }
-
- //含有绝对路径的输出
- File[] fileps=file.listFiles();
- for (File file1:fileps
- ) {
- System.out.println(file1);
-
- }
- }
- /*
- public boolean renameTo(File dest):把文件重命名为指定的文件路径
- 比如:file1.renameTo(file2):
- 要想保证返回true,需要file1在硬盘中是存在的,且file2不能再硬盘中存在
- */
- @Test
- public void test3(){
- File file1 = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
- File file2=new File("D:\\WenJian\\IDEA\\fghj\\aa.txt");
-
- boolean b = file2.renameTo(file1);
- System.out.println(b);
- }
-
-
- /*
- public boolean isDirectory():判断是否是文件目录
- public boolean isFile() :判断是否是文件
- public boolean exists() :判断是否存在
- public boolean canRead() :判断是否可读
- public boolean canWrite() :判断是否可写
- public boolean isHidden() :判断是否隐藏
- */
- @Test
- public void test4(){
- File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
- System.out.println(file.isDirectory());
- System.out.println(file.isFile());
- System.out.println(file.exists());
- System.out.println(file.canRead());
- System.out.println(file.canWrite());
- System.out.println(file.isHidden());
-
- System.out.println();
- File file1 = new File("D:\\WenJian\\IDEA");
- System.out.println(file1.isDirectory());
- System.out.println(file1.isFile());
- System.out.println(file1.exists());
- System.out.println(file1.canRead());
- }
-
-
- /*
- public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
- public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。
- 如果此文件目录的上层目录不存在,也不创建。
- public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
- 注意事项:如果你创建文件或者文件目录没有写盘符路径,那么,默认在项目
- 路径下。
- public boolean delete():删除文件或者文件夹
- 删除注意事项:
- Java中的删除不走回收站。
- 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录
- */
- @Test
- public void test5() throws IOException {
-
- File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello1.txt");
- if(!file.exists()){
- file.createNewFile();
- System.out.println("创建成功");
- }else {
- file.delete();
- System.out.println("删除成功");
- }
-
-
-
- }
-
- // D:\WenJian\IDEA\fghj
- // public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。
- //如果此文件目录的上层目录不存在,也不创建。
- @Test
- public void test6(){
-
- File file1 = new File("D:\\WenJian\\IDEA\\fghj");
- File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
- if (!file1.exists()) {
- file1.mkdir();
- System.out.println("创建成功");
- }else {
- file1.delete();
- System.out.println("删除成功");
- }
- file.delete();
- }
- /**
- *
- * @description:
- * @author ----千里之行,始于足下----
- * @date 2022/8/22 18:59
- */
- @Test
- public void test7() throws IOException {
- File file = new File("D:\\hello.txt");
- if (!file.exists()) {
- file.createNewFile();
- }
- File file1 = new File(file.getParent(), "hello.txt");
- System.out.println(file);
- file.delete();
- }
-
- @Test
- public void test8(){
- File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分");
- String[] files=file.list();
- for (String fle :
- files) {
- int length = fle.length();
- // if(fle.substring(length-3).equals("jpg")){
- if(fle.endsWith("jpg")){
- System.out.println(fle);
- }
- }
- }
- }
/** * 1.1、标准的输入输出流 * System.in:标准的输入流,默认从键盘输入 * System.out:标准的输出流,默认从控制台输出 * 1.2 * System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入输出的流 * * 练习: * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续 * 进行输入操作,直至当输入“e”或者“exit”时,退出程序。 * * @description: * @author ----千里之行,始于足下---- * @date 2022/9/16 20:25 */
- public class FileTest2 {
-
- public static void main(String[] args) {
- BufferedReader br = null;
-
-
- try {
- InputStreamReader isr = new InputStreamReader(System.in);
- br = new BufferedReader(isr);
- while (true) {
- System.out.println("请输入字字符串");
- String data = null;
- data = br.readLine();
- if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
- System.out.println("程序结束");
- break;
- }
- String upperCase = data.toUpperCase();
- System.out.println(upperCase);
- }
- }catch(IOException e){
- e.printStackTrace();
- }finally{
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
-