Java 输入输出流实验

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 08:52   675   0

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

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

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

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

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

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

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP