扫描程序将接受用户输入,但找不到文件 [英] Scanner will take user input but will not find the file

查看:61
本文介绍了扫描程序将接受用户输入,但找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个程序,用于读取文件并打印出一些已编辑文本的文件.该代码将编译问题,因为它将读取用户输入,但会说文件在那里时找不到文件.我觉得我想念一些东西.我是新手,所以对我轻松一点.

This is a program to read a file and print out the file with some of the text edited. The code will compile the issue is that it will read the users input but will say file is not found when the file is there. I feel like I am missing something. I am brand new at this so go easy on me.

推荐答案

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MainTest {

    public static void main(String args[]) {
      //  if (args[0] != null)
            readFile();
    }

    public static void readFile() { // Method to read file

        Scanner inFile = null;
        String out = "";
        try {
            Scanner input = new Scanner(System.in);
            System.out.println("enter file name");
            String filename = input.next();
            File in = new File(filename); // ask for the file name
            inFile = new Scanner(in);


            int count = 0;
            while (inFile.hasNextLine()) { // reads each line
                String line = inFile.nextLine();


                for (int i = 0; i < line.length(); i++) {
                    char ch = line.charAt(i);
                    out = out + ch;

                    if (ch == '{') {
                        count = count + 1;
                        out = out + " " + count;
                    } else if (ch == '}') {
                        out = out + " " + count;
                        if (count > 0) {
                            count = count - 1;
                        }
                    }
                }
            }
            System.out.println(out);
        } catch (FileNotFoundException exception) {
            System.out.println("File not found.");
        }
        inFile.close();
    }
}

这篇关于扫描程序将接受用户输入,但找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆