1.列印命令列參數
Code:
- import java.io.*;
- public class Test1 {
- public static void main(String[] args) {
- for(int i=0; i<args.length; i++) {
- System.out.print(args[i]);
- }
- System.out.println();
- }
- }
2.把一個檔案中的內容原樣輸出到控制台
Code:
- import java.io.*;
-
- public class Test2 {
- public static void main(String[] args) {
- System.out.println("Please enter the file path:");
- try {
- String fileName = "";
- while(true) {
- int readByte = System.in.read();
- if(readByte==-1 ||readByte == '/r') {
- break;
- } else {
- fileName += (char)readByte;
- }
- }
- char[] buffer = new char[20];
- FileReader fr = new FileReader(fileName);
- while(true) {
- int length = fr.read(buffer);
- if(length<0) {
- break;
- } else {
- String text = new String(buffer, 0, length);
- System.out.print(text);
- }
- }
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
3.把一個檔案中的內容寫到另外一個檔案中
Code:
- import java.io.*;
-
- public class Test3 {
- public static void main(String[] args) {
- FileRWTest frw = new FileRWTest();
- }
- }
-
- class FileRWTest {
- BufferedReader br;
- BufferedWriter bw;
- File in = new File("input.txt");
- File out = new File("output.txt");
- char[] cBuf = new char[1024];
- int off=0;
- public FileRWTest() {
- try {
- if(!in.exists()) {
- in.createNewFile();
- }
- if(!out.exists()) {
- out.createNewFile();
- }
- br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));
- while(br.readLine()!=null) {
- br.read(cBuf,off,off+1024);
- bw.write(cBuf,off,off+1024);
- off += 1024;
- }
- br.close();
- bw.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
4.把水仙花數輸出到一個檔案中
Code:
- import java.io.*;
-
- public class Test4 {
- public static void main(String[] args) {
- try {
- byte[] by;
- FileOutputStream fos = new FileOutputStream("data.txt",true);
- for(int i=100; i<=999; i++) {
- int a = i/100;
- int b = i%100/10;
- int c = i%10;
- if(i == (int)(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3))) {
- String s = new String(i+"="+a+"*"+a+"*"+a+
- "+"+b+"*"+b+"*"+b+
- "+"+c+"*"+c+"*"+c+"/n");
-
- by = s.getBytes();
- fos.write(by,0,by.length);
- } else {
- continue;
- }
- }
- fos.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
5.把一個檔案中的內容讀出,把大寫字母轉換成小寫字母后輸出到另一個檔案中
Code:
- import java.io.*;
-
- public class Test5 {
- static BufferedReader br;
- static BufferedWriter bw;
- static File in = new File("Test5.java");
- static File out = new File("result.txt");
- static String str1;
- static String str2;
- public static void main(String[] args) {
- try {
- if(!in.exists()) {
- in.createNewFile();
- }
- if(!out.exists()) {
- out.createNewFile();
- }
- br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));
- while((str1 = br.readLine())!=null) {
- str2 = str1.toLowerCase();
- bw.write(str2, 0, str2.length());
- bw.newLine();
- }
- br.close();
- bw.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }