现在的位置: 首页 > 综合 > 正文

Java 输入输出流实验

2013年08月26日 ⁄ 综合 ⁄ 共 4311字 ⁄ 字号 评论关闭

1.打印命令行参数

Code:
  1. import java.io.*;   
  2. public class Test1 {   
  3.     public static void main(String[] args) {   
  4.         for(int i=0; i<args.length; i++) {   
  5.             System.out.print(args[i]);   
  6.         }   
  7.         System.out.println();   
  8.     }   
  9. }  

2.把一个文件中的内容原样输出到控制台

Code:
  1. import java.io.*;   
  2.   
  3. public class Test2 {   
  4.     public static void main(String[] args) {   
  5.         System.out.println("Please enter the file path:");   
  6.         try {   
  7.             String fileName = "";   
  8.             while(true) {   
  9.                 int readByte = System.in.read();   
  10.                 if(readByte==-1 ||readByte == '/r') {   
  11.                     break;   
  12.                 } else {   
  13.                     fileName += (char)readByte;   
  14.                 }   
  15.             }   
  16.             char[] buffer = new char[20];   
  17.             FileReader fr = new FileReader(fileName);   
  18.             while(true) {   
  19.                 int length = fr.read(buffer);   
  20.                 if(length<0) {   
  21.                     break;   
  22.                 } else {   
  23.                     String text = new String(buffer, 0, length);   
  24.                     System.out.print(text);   
  25.                 }    
  26.             }   
  27.         } catch(IOException e) {   
  28.             e.printStackTrace();   
  29.         }   
  30.     }   
  31. }  

3.把一个文件中的内容写到另外一个文件中

Code:
  1. import java.io.*;   
  2.   
  3. public class Test3 {   
  4.     public static void main(String[] args) {   
  5.         FileRWTest frw = new FileRWTest();   
  6.     }   
  7. }   
  8.   
  9. class FileRWTest {   
  10.     BufferedReader br;   
  11.     BufferedWriter bw;   
  12.     File in = new File("input.txt");   
  13.     File out = new File("output.txt");   
  14.     char[] cBuf = new char[1024];   
  15.     int off=0;   
  16.     public FileRWTest() {   
  17.         try {   
  18.             if(!in.exists()) {   
  19.                 in.createNewFile();   
  20.             }   
  21.             if(!out.exists()) {   
  22.                 out.createNewFile();   
  23.             }   
  24.             br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));   
  25.             bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));   
  26.             while(br.readLine()!=null) {   
  27.                 br.read(cBuf,off,off+1024);   
  28.                 bw.write(cBuf,off,off+1024);   
  29.                 off += 1024;   
  30.             }   
  31.             br.close();   
  32.             bw.close();   
  33.         } catch(IOException e) {   
  34.             e.printStackTrace();   
  35.         }   
  36.     }   
  37. }  

4.把水仙花数输出到一个文件中

Code:
  1. import java.io.*;   
  2.   
  3. public class Test4 {   
  4.     public static void main(String[] args) {   
  5.         try {   
  6.             byte[] by;   
  7.         FileOutputStream fos = new FileOutputStream("data.txt",true);   
  8.         for(int i=100; i<=999; i++) {   
  9.             int a = i/100;   
  10.                 int b = i%100/10;   
  11.                 int c = i%10;   
  12.                 if(i == (int)(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3))) {   
  13.                     String s = new String(i+"="+a+"*"+a+"*"+a+   
  14.                                             "+"+b+"*"+b+"*"+b+   
  15.                                             "+"+c+"*"+c+"*"+c+"/n");   
  16.                    
  17.                     by = s.getBytes();   
  18.                     fos.write(by,0,by.length);   
  19.                 } else {   
  20.                 continue;   
  21.             }   
  22.         }   
  23.         fos.close();   
  24.     } catch(IOException e) {   
  25.         e.printStackTrace();   
  26.     }   
  27.   }   
  28. }  

5.把一个文件中的内容读出,把大写字母转换成小写字母后输出到另一个文件中

Code:
  1. import java.io.*;   
  2.   
  3. public class Test5 {   
  4.     static BufferedReader br;   
  5.     static BufferedWriter bw;   
  6.     static File in = new File("Test5.java");   
  7.     static File out = new File("result.txt");   
  8.     static String str1;   
  9.     static String str2;   
  10.     public static void main(String[] args) {   
  11.             try {   
  12.                 if(!in.exists()) {   
  13.                     in.createNewFile();   
  14.                 }   
  15.                 if(!out.exists()) {   
  16.                     out.createNewFile();   
  17.                 }   
  18.                 br = new BufferedReader(new InputStreamReader(new FileInputStream(in)));   
  19.                 bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));   
  20.                 while((str1 = br.readLine())!=null) {   
  21.                     str2 = str1.toLowerCase();   
  22.                     bw.write(str2, 0, str2.length());   
  23.                     bw.newLine();   
  24.                 }   
  25.                 br.close();   
  26.                 bw.close();   
  27.             } catch(IOException e) {   
  28.                 e.printStackTrace();   
  29.             }      
  30.         }   
  31. }  

抱歉!评论已关闭.