IOに実装JAVA学習(4)-----牛のゲストは、アプレットのテーマをストリーム

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/q303705455/article/details/98385426

質問今回はオフ牛の興味深い質問をたくさん見つけてください、これは主に実現されているIOストリームプログラムのテーマに関連します。

  • すべてのパス指定した出力ファイルのトラバース
    。1.実行されているパスを入力するようユーザに要求するユーティリティクラスを定義します。ツールは(とそのサブディレクトリ)のすべてのファイルは、パスの下に表示されます。
import java.io.File;
import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.F2D;
//遍历并输出文件
public class Tool1 {

	public static void main(String[] args) {
		System.out.println("please enter a path:");
		String path;
		Scanner sc = new Scanner(System.in);
		path = sc.next();
		File file = new File(path);
		Dir(file);

	}

	public static void Dir(File file) {

		File[] file1 = file.listFiles();
		for (File f : file1) {
			if (f.isDirectory()) {
				Dir(f);
			}
			System.out.println(f.getName());

		}
	}

}

  • 通って、出力ファイルとフォルダの数
    2.ツールクラスを定義し、そのような入力は、ユーザーがプログラムを実行するためのパスが必要です。ツールは、ファイルパス、カウントされたフォルダの数になります。
import java.io.File;
import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.F2D;
//遍历并输出文件文件夹个数
public class Tool {
	private static int n = 0;
	private static int n1 = 0;

	public static void main(String[] args) {
		System.out.println("please enter a path:");
		String path;
		Scanner sc = new Scanner(System.in);
		path = sc.next();
		File file = new File(path);
		Dir(file);

		System.out.println(n-n1);
		System.out.println(n1);
	}

	public static void Dir(File file) {

		File[] file1 = file.listFiles();
		for (File f : file1) {
			if (f.isDirectory()) {
				n1++;
				Dir(f);
			}
			n++;

		}
	}
	
}

  • ファイルを保存して、文字ストリーム読み込むためのコンソール
    3.メモ帳アプレット模造コンソール入力を
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;

import org.junit.Test;

public class Tool2 {
	static String path ;
	
//控制台输入的仿记事本小程序
	public static void main(String[] args) {

	}

	@Test
	public void out() {
		Scanner sc = new Scanner(System.in);
		System.out.println("please enter the note path:");
		path = sc.next();
		BufferedReader br = null;
		PrintWriter pw = null;
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			pw = new PrintWriter(path);
			System.out.println("-------------------记事本----------------------");
			String str = null;

			while (!(str = br.readLine()).equals("exit")) {
				pw.write(str+"\n");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e2) {
					e2.printStackTrace();
				}
			}
			if (pw != null) {
				pw.close();
			}
		}

	}

	@Test
	public void in() {
		Scanner sc = new Scanner(System.in);
		System.out.println("please enter the note which you want to open:");
		path = sc.next();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
			String str = null;
			while((str = br.readLine()) != null) {
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

おすすめ

転載: blog.csdn.net/q303705455/article/details/98385426