Java 常用方法实例 Stream,File,IO

从控制台读取Char

import java.io.*;

public class 从控制台读取char {
  public static void main(String[] args) throws IOException{
    // 使用 System.in 创建 BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    char c;
    System.out.println("输入字符, 按下 'q' 键退出。");
    // 读取字符
    do {
      c = (char) br.read();
      System.out.print(c);
    } while (c != 'q');
  }
}

执行结果

输入字符, 按下 'q' 键退出。
www.weijun901.com
www.weijun901.com
q
q
进程已结束,退出代码0

从控制台读取String

结果都差不多,多少还是有点区别

import java.io.*;

public class 从控制台读取String {
  public static void main(String[] args) throws IOException{
    // 使用 System.in 创建 BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    System.out.println("输入字符串,按下 'end' 退出。");
    // 读取字符串
    do {
      str = br.readLine();
      System.out.print(str);
    } while (!str.equals("end"));
  }
}

执行结果

输入字符, 按下 'end' 键退出。
www.weijun901.com
www.weijun901.com
end
end
进程已结束,退出代码0

创建文件

import java.io.*;
import java.util.Scanner;

public class 创建文件 {
  public static void main(String[] args) throws IOException {
    //从键盘接收[文件名]
    System.out.println("请输入要创建的文件名:");
    Scanner scan = new Scanner(System.in);
    try {
      if (scan.hasNext()) {
        String nf = scan.next();

        // 创建[文件]
        File file = new File(nf);

        // 构建FileOutputStream对象
        FileOutputStream fop = new FileOutputStream(file);

        // 关闭输出流,释放系统资源
        fop.close();
      }
      scan.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

执行结果

请输入要创建的文件名:
weijun.txt

进程已结束,退出代码0

创建目录

import java.io.*;
import java.util.Scanner;

public class 创建目录 {
  public static void main(String[] args) throws IOException {
    //从键盘接收[目录名]
    System.out.println("请输入要创建的目录名:");
    Scanner scan = new Scanner(System.in);
    try {
      if (scan.hasNext()) {
        String nl = scan.next();

        // 创建[目录]
        File file = new File(nl);
        file.mkdir();

        // 构建FileOutputStream对象
        FileOutputStream fop = new FileOutputStream(file);

        // 关闭输出流,释放系统资源
        fop.close();
      }
      scan.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

执行结果

请输入要创建的目录名:
weijun

进程已结束,退出代码0

读取文件

import java.io.*;
import java.util.Scanner;

public class 读取文件 {
  public static void main(String[] args) throws IOException{
    //从键盘接收[文件名]
    System.out.println("请输入要打开的文件名:");
    Scanner scan = new Scanner(System.in);
    try {
      if (scan.hasNext()){
        String rf = scan.next();

        // 构建FileInputStream对象,文件不存在会自动新建
        FileInputStream fip = new FileInputStream(rf);

        // 构建InputStreamReader对象,参数可以指定编码,默认为UTF-8
        InputStreamReader reader = new InputStreamReader(fip, "UTF-8");

        // 构建StringBuffer对象
        StringBuffer sb = new StringBuffer();

        while (reader.ready()) {
          // 追加到StringBuffer对象中
          sb.append((char)reader.read());
        }
        System.out.println(sb);
        reader.close();
        fip.close();
      }
      scan.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

执行结果

请输入要打开的文件名:
weijun.txt
www.weijun901.com

进程已结束,退出代码0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值