鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 寂寞知己

输入的文件名,打开该文件并将文件的内容输出到屏幕上(求助)

[复制链接]
发表于 2021-12-4 19:45:37 | 显示全部楼层
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.Scanner;

  6. public class Main {
  7.         static Scanner input = new Scanner(System.in);

  8.         public static void main(String[] args) {
  9.                 while (true) {
  10.                         try {
  11.                         System.out.print("请输入文件路径及文件名:");
  12.                         String Filename = input.next();
  13.                         File f = new File(Filename);
  14.                         if (f.exists()) {
  15.                                 FileInputStream in = new FileInputStream(f);
  16.                                 int n;
  17.                                 byte[] by = new byte[999999999];
  18.                                 System.out.println("正在读取···");
  19.                                 n = in.read(by);
  20.                                 System.out.println(new String(by,0,n));
  21.                         }else {
  22.                                 System.out.println("文件不存在。");
  23.                                 continue;
  24.                         }}catch(FileNotFoundException e) {
  25.                                 System.out.println("文件不存在或拒绝访问。");
  26.                         }catch(IOException e) {
  27.                                 System.out.println("文件读写异常。");
  28.                         }
  29.                 }
  30.         }
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 11:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表