[关闭]
@Senl 2017-03-24T03:19:57.000000Z 字数 1066 阅读 1188

3.24 学习进度

技术学习


Java

一个小的处理输入的玩意

Test类 处理输入和写进文件

  1. import java.io.*;
  2. import java.util.*;
  3. public class Test {
  4. public static String getIn(){
  5. Scanner sc = new Scanner(System.in);
  6. String input = sc.nextLine();
  7. return input;
  8. }
  9. public static int getInt(){
  10. Scanner s = new Scanner(System.in);
  11. int i = s.nextInt();
  12. return i;
  13. }
  14. public static void Write(String fileName, String context ){
  15. try {
  16. // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  17. FileWriter writer = new FileWriter(fileName, true);
  18. writer.write(context);
  19. writer.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }

TestDrive类 启动Test并且判断有无文件存在

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class TestDrive {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. String fileName = "F:/Ex/Test.txt";
  7. File file=new File(fileName);
  8. if(!file.exists())
  9. {
  10. try {
  11. file.createNewFile();
  12. } catch (IOException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }
  17. System.out.println("you can enter sth now"
  18. + "enter 'exit' to stop");
  19. String input = Test.getIn();
  20. while(!input.equals("exit")){
  21. Test.Write(fileName, input+"\r\n");
  22. input = Test.getIn();
  23. }
  24. }
  25. }

测试结果

-见博客

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注