Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件中的Java输出_Java_File_Java.util.scanner - Fatal编程技术网

文件中的Java输出

文件中的Java输出,java,file,java.util.scanner,Java,File,Java.util.scanner,我对Java有点陌生,运行这个程序,它要求我输入文件名,读取文件中的信息,用它填充数组,然后要求我输入要输出的文件名。 然而,当我为输出请求文件名时,它从未正确读取它,我也不知道为什么。 我正在使用扫描仪,我现在就是这样编码的: System.out.println("What file name?"); Scanner teclat = null; try { teclat = new Scanner(System.in); } catch (E

我对Java有点陌生,运行这个程序,它要求我输入文件名,读取文件中的信息,用它填充数组,然后要求我输入要输出的文件名。 然而,当我为输出请求文件名时,它从未正确读取它,我也不知道为什么。 我正在使用扫描仪,我现在就是这样编码的:

    System.out.println("What file name?");
    Scanner teclat = null;
    try {
        teclat = new Scanner(System.in);
    } catch (Exception e) {
        System.err.println("Wrong name!");
        System.exit(-1);
    }
    String fitxer = teclat.toString();
    PrintWriter surtida = new PrintWriter(new FileWriter(fitxer)); 
    teclat.close();
    String fitxer = teclat.next().trim();
然后我进行打印:

if(this.esBuit()) 
            System.err.println("Llista de llenguatges buida!");
        else {
            Node p = this.buscarNode(lleng);    // Busquem el Llenguatge  lleng  i imprimim tots els que han estat influenciats per aquest
            System.out.println("- - - - - - -\n");
            System.out.println("Llenguatges influenciats per "+lleng+":");
            while(p.inf != null) {
                System.out.print(p.inf.nom+"\n");
                    p = p.inf;
            }
            System.out.println("- - - - - - -");
        }
但是,如果我尝试使用
System.out.printnl
打印第一段代码后的文件名,它会打印一个奇怪的引用,就好像它没有正确读取文件名一样。 我怎样才能让它工作

编辑:我更新了我的代码,并添加了一个可能相关的新问题: 我试过了,什么也没变。我正在尝试其他方法,在程序开始时,当我实际输入文件名时,它读取第一行并执行以下操作:

String s = fitxer.toString();
        String b1 = s.substring(0,11);
        System.out.println(b1);
        String b2 = s.substring(0,14);
        System.out.println(b2);
这张照片是:

java.util.S
java.util.Scan

为什么?

当您开始从输入读取时:

String fitxer = teclat.toString();
应该是

String fitxer = teclat.nextLine();
这是因为将对象转换为字符串表示形式。另一方面,实际上从扫描仪指向的输入中读取一行

应该是这样的:

System.out.println("What file name?");
Scanner teclat = null;
try {
    teclat = new Scanner(System.in);
} catch (Exception e) {
    System.err.println("Wrong name!");
    System.exit(-1);
}
String fitxer = teclat.nextLine();
PrintWriter surtida = new PrintWriter(new FileWriter(fitxer)); 
teclat.close();

当您开始从输入读取时:

String fitxer = teclat.toString();
应该是

String fitxer = teclat.nextLine();
这是因为将对象转换为字符串表示形式。另一方面,实际上从扫描仪指向的输入中读取一行

应该是这样的:

System.out.println("What file name?");
Scanner teclat = null;
try {
    teclat = new Scanner(System.in);
} catch (Exception e) {
    System.err.println("Wrong name!");
    System.exit(-1);
}
String fitxer = teclat.nextLine();
PrintWriter surtida = new PrintWriter(new FileWriter(fitxer)); 
teclat.close();

当您开始从输入读取时:

String fitxer = teclat.toString();
应该是

String fitxer = teclat.nextLine();
这是因为将对象转换为字符串表示形式。另一方面,实际上从扫描仪指向的输入中读取一行

应该是这样的:

System.out.println("What file name?");
Scanner teclat = null;
try {
    teclat = new Scanner(System.in);
} catch (Exception e) {
    System.err.println("Wrong name!");
    System.exit(-1);
}
String fitxer = teclat.nextLine();
PrintWriter surtida = new PrintWriter(new FileWriter(fitxer)); 
teclat.close();

当您开始从输入读取时:

String fitxer = teclat.toString();
应该是

String fitxer = teclat.nextLine();
这是因为将对象转换为字符串表示形式。另一方面,实际上从扫描仪指向的输入中读取一行

应该是这样的:

System.out.println("What file name?");
Scanner teclat = null;
try {
    teclat = new Scanner(System.in);
} catch (Exception e) {
    System.err.println("Wrong name!");
    System.exit(-1);
}
String fitxer = teclat.nextLine();
PrintWriter surtida = new PrintWriter(new FileWriter(fitxer)); 
teclat.close();


我不确定它是否相关,但在
PrintWriter surtida=new PrintWriter(new FileWriter(fitxer))行中会出现一条警告,正好在“surtida”,它告诉我添加suppressWarning,知道为什么会出现吗?我尝试了你的建议,它在我更改的通道中显示了这个错误:
线程“main”java.util.NoSuchElementException中的异常:找不到行
NoSuchElementException
意味着没有输入行,可能是空行?问题是,我只能输入要读取的文件名,然后当它使用信息时,一些异常跳转,然后它直接转到这个方法,它打印System.out.printnl但我不能键入,就像它读取我没有键入的内容一样DHMM,尝试使用
teclat.next().trim()
而不是
teclat.nextLine()
。我使用了它,但没有任何更改。我更新了关于主要问题的代码,我希望您能帮助我,我不确定这是否相关,但在
PrintWriter surtida=new PrintWriter(new FileWriter(fitxer))行中会出现警告,正好在“surtida”,它告诉我添加suppressWarning,知道为什么会出现吗?我尝试了你的建议,它在我更改的通道中显示了这个错误:
线程“main”java.util.NoSuchElementException中的异常:找不到行
NoSuchElementException
意味着没有输入行,可能是空行?问题是,我只能输入要读取的文件名,然后当它使用信息时,一些异常跳转,然后它直接转到这个方法,它打印System.out.printnl但我不能键入,就像它读取我没有键入的内容一样DHMM,尝试使用
teclat.next().trim()
而不是
teclat.nextLine()
。我使用了它,但没有任何更改。我更新了关于主要问题的代码,我希望您能帮助我,我不确定这是否相关,但在
PrintWriter surtida=new PrintWriter(new FileWriter(fitxer))行中会出现警告,正好在“surtida”,它告诉我添加suppressWarning,知道为什么会出现吗?我尝试了你的建议,它在我更改的通道中显示了这个错误:
线程“main”java.util.NoSuchElementException中的异常:找不到行
NoSuchElementException
意味着没有输入行,可能是空行?问题是,我只能输入要读取的文件名,然后当它使用信息时,一些异常跳转,然后它直接转到这个方法,它打印System.out.printnl但我不能键入,就像它读取我没有键入的内容一样DHMM,尝试使用
teclat.next().trim()
而不是
teclat.nextLine()
。我使用了它,但没有任何更改。我更新了关于主要问题的代码,我希望您能帮助我,我不确定这是否相关,但在
PrintWriter surtida=new PrintWriter(new FileWriter(fitxer))行中会出现警告,正好在“surtida”,它告诉我添加suppressWarning,知道为什么会出现吗?我尝试了你的建议,它在我更改的通道中显示了这个错误:
线程“main”java.util.NoSuchElementException中的异常:找不到行
NoSuchElementException
意味着没有输入行,可能是空行?问题是,我只能输入要读取的文件名,然后当它使用信息时,一些异常跳转,然后它直接转到这个方法,它打印System.out.printnl但我不能键入,就像它读取我没有键入的内容一样DHMM,尝试使用
teclat.next().trim()
而不是
teclat.nextLine()
。我使用了它,但没有任何更改。我更新了关于主要问题的代码,希望你能帮助我