zoukankan      html  css  js  c++  java
  • Java 遍历文件夹,文件读写

    遍历文件夹,输出文件夹下的所有文件和文件名:

    import java.io.File;
    
    public class Scaner {
    
    	public static void main(String[] args) {
    		printFiles(new File("E:\practice\ReadFileProperty"), 1);
    	}
    	public static void printFiles(File dir,int tab) {
    		if(dir.isDirectory()) {
    			File next[]=dir.listFiles();
    			for (int i = 0; i < next.length; i++) {
    				for (int j = 0; j < tab; j++) {
    					System.out.print("|--");
    				}
    				System.out.println(next[i].getName());
    				if(next[i].isDirectory()) {
    					printFiles(next[i], tab+1);
    				}
    			}
    		}
    	}
    }
    

     输出:

    |--.classpath
    |--.project
    |--bin
    |--|--ReadFileProperty.class
    |--|--Scaner.class
    |--src
    |--|--ReadFileProperty.java
    |--|--Scaner.java
    |--test.txt
    

    文件的读写操作:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    
    public class readFile {
    
    	public static void main(String[] args) {
    		File file=new File("test.txt");
    		if(file.exists()) {
    			System.out.println("文件存在");
    			
    			try {
    				FileInputStream fis=new FileInputStream(file);
    				InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
    				BufferedReader br=new BufferedReader(isr);
    				 
    				String line;
    				while((line=br.readLine())!=null) {
    					System.out.println(line);
    				}
    				br.close();
    				isr.close();
    				fis.close();
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			} catch (UnsupportedEncodingException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		try {
    			File newFile=new File("newText.txt");
    			FileOutputStream fos=new FileOutputStream(newFile);
    			OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
    			BufferedWriter bw=new BufferedWriter(osw);
    			
    			bw.write("长歌行
    ");
    			bw.write("青青园中葵,朝露待日晞。
    ");
    			bw.write("阳春布德泽,万物生光辉。
    ");
    			bw.write("常恐秋节至,焜黄华叶衰。
    ");
    			bw.write("百川东到海,何时复西归? 
    ");
    			bw.write("少壮不努力,老大徒伤悲。
    ");
    			
    			bw.close();
    			osw.close();
    			fos.close();
    			
    			System.out.println("写入完成");
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    }
    

     程序输出文件test.txt中的内容,并且把内容写入到新文件newText.txt中。

  • 相关阅读:
    Ubuntu 16.04
    每天一道LeetCode--389. Find the Difference
    每天一道LeetCode--371. Sum of Two Integers
    Ubuntu 16.04 小飞机启动失败
    每天一道LeetCode--344. Reverse String
    leetcode1458 Max Dot Product of Two Subsequences
    CF1313C2 Skyscrapers (hard version)
    CF1295C Obtain The String
    CF1251D Salary Changing
    CF1286A Garland
  • 原文地址:https://www.cnblogs.com/zhhy236400/p/10477428.html
Copyright © 2011-2022 走看看