Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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 - Fatal编程技术网

用Java从外部文件打印文本

用Java从外部文件打印文本,java,Java,我试图创建一个包含一行文本的外部文件,然后在Java程序中打印出这行文本。我试过了;然而,我遇到了麻烦。我的代码没有抛出任何错误,只是工作不正常 import java.io.*; import java.util.*; public class App { public static void main(String[] args) { String terminate = "X"; String Question = ""; System.out.println

我试图创建一个包含一行文本的外部文件,然后在Java程序中打印出这行文本。我试过了;然而,我遇到了麻烦。我的代码没有抛出任何错误,只是工作不正常

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

public class App {

public static void main(String[] args) {

    String terminate = "X";
    String Question = "";
    System.out.println("I am the all-knowing Magic 8 Ball!");

    do {
        Scanner scnr = new Scanner(System.in);
        System.out.println("");
        System.out.println("Ask your question here or enter 'X' to exit:");
        Question = scnr.nextLine();
        continueGame(Question); 
    } while (terminate != Question);


        String testData[] = {"test1", "test2", "test3"};
        writeFile(testData, "data.txt");
        ArrayList<String> fileContents = readFile("data.txt");

        if (terminate.equalsIgnoreCase(Question)) {
        for (String contents : fileContents) {
            System.out.println(contents);
        }        
    }
   }

public static void continueGame(String Question) {

    char terminate = 'X';
    char condition = Question.charAt(0);

    if (condition == terminate) 
    {
        System.out.println("");
        System.out.println("Thanks for playing!");
        System.exit(0);
    }
    try 
    {
        Random rand = new Random();
        int choice;
        choice = 1 + rand.nextInt(20);
        responseOptions(choice, Question);
    }
    catch (Exception e)
    {
        System.out.println("Error: Invalid");
    }
}

public static void responseOptions(int choice, String answer)
{
    switch (choice)
    {
        case 1: answer = "Response: It is certain"; break;
        case 2: answer = "Response: It is decidely so"; break;
        case 3: answer = "Response: Without a doubt"; break;
        case 4: answer = "Response: Yes, definitely"; break;
        case 5: answer = "Response: You may rely on it"; break;
        case 6: answer = "Response: As I see it, yes"; break;
        case 7: answer = "Response: Most likely"; break;
        case 8: answer = "Response: Outlook good"; break;
        case 9: answer = "Response: Yes"; break;
        case 10: answer = "Response: Signs point to yes"; break;
        case 11: answer = "Response: Reply hazy, try again"; break;
        case 12: answer = "Response: Ask again later"; break;
        case 13: answer = "Response: Better not tell you now"; break;
        case 14: answer = "Response: Cannot predict now"; break;
        case 15: answer = "Response: Concentrate and ask again"; break;
        case 16: answer = "Response: Don't count on it"; break;
        case 17: answer = "Response: My reply is no"; break;
        case 18: answer = "Response: My sources say no"; break;
        case 19: answer = "Response: Outlook not so good"; break;
        case 20: answer = "Response: Very doubtful"; break;
    }
    System.out.println("");
    System.out.println(answer);
}

public static void writeFile(String arrayToWrite[], String filename) {

    try {
        PrintWriter wordWriter = new PrintWriter("filename.txt");
        for (String words : arrayToWrite) {
            wordWriter.println(words + "\n");
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        System.out.print(msg);
    }   
}

public static ArrayList<String> readFile(String filename) {
    ArrayList<String> fileContents = new ArrayList();
    File myFile = new File(filename);

    try {
        Scanner scnr = new Scanner(myFile);
        String tempStr = "";
        while (scnr.hasNextLine()) {
            tempStr = scnr.nextLine();
            fileContents.add(tempStr);
        }
    } catch (Exception e) {
        String msg = e.getMessage();
        System.out.println(msg);
    }
    return fileContents;
}  
}
import java.io.*;
导入java.util.*;
公共类应用程序{
公共静态void main(字符串[]args){
字符串终止=“X”;
字符串问题=”;
System.out.println(“我是无所不知的魔法8球!”);
做{
扫描仪scnr=新扫描仪(System.in);
System.out.println(“”);
System.out.println(“在此处提问或输入'X'退出:”;
问题=scnr.nextLine();
连续名称(问题);
}while(终止!=问题);
字符串testData[]={“test1”、“test2”、“test3”};
writeFile(testData,“data.txt”);
ArrayList fileContents=readFile(“data.txt”);
如果(终止同等信号案例(问题)){
for(字符串内容:fileContents){
系统输出打印项次(内容);
}        
}
}
公共静态void continueGame(字符串问题){
字符终止='X';
字符条件=问题.charAt(0);
如果(条件==终止)
{
System.out.println(“”);
System.out.println(“谢谢玩!”);
系统出口(0);
}
尝试
{
Random rand=新的Random();
智力选择;
选择=1+兰特·耐克斯汀(20);
回答(选择、问题);
}
捕获(例外e)
{
System.out.println(“错误:无效”);
}
}
公共静态void responseOptions(整数选择,字符串应答)
{
开关(选择)
{
案例1:回答=“回答:确定”中断;
案例2:answer=“Response:确实如此”中断;
案例3:回答=“回答:毫无疑问”中断;
案例4:answer=“回答:是的,绝对”中断;
案例5:answer=“Response:你可以信赖它”中断;
案例6:answer=“Response:在我看来,是的”中断;
案例7:回答=“响应:最有可能”中断;
案例8:answer=“Response:Outlook good”中断;
案例9:回答=“回答:是”中断;
案例10:回答=“响应:符号指向是”中断;
案例11:answer=“Response:Reply hazy,重试”中断;
案例12:answer=“Response:稍后再问”中断;
案例13:answer=“Response:现在最好不要告诉你”中断;
案例14:回答=“回答:现在无法预测”中断;
案例15:answer=“Response:集中注意力,再次提问”休息;
案例16:answer=“Response:不要指望它”中断;
案例17:answer=“Response:我的回答是no”中断;
案例18:answer=“Response:我的消息来源说不”中断;
案例19:answer=“Response:前景不太好”中断;
案例20:回答=“回答:非常可疑”中断;
}
System.out.println(“”);
System.out.println(应答);
}
公共静态void writeFile(字符串arrayToWrite[],字符串文件名){
试一试{
PrintWriter wordWriter=新的PrintWriter(“filename.txt”);
for(字符串:arrayToWrite){
wordWriter.println(words+“\n”);
}
}捕获(例外e){
字符串msg=e.getMessage();
系统输出打印(msg);
}   
}
公共静态ArrayList读取文件(字符串文件名){
ArrayList fileContents=新的ArrayList();
File myFile=新文件(文件名);
试一试{
扫描仪scnr=新扫描仪(myFile);
字符串tempStr=“”;
while(scnr.hasNextLine()){
tempStr=scnr.nextLine();
fileContents.add(tempStr);
}
}捕获(例外e){
字符串msg=e.getMessage();
System.out.println(msg);
}
返回文件内容;
}  
}

代码的问题是,尽管您在
writeFile()
中获得了
文件名
,但您创建的文件名不是
文件名
,而是
文件名.txt
。在
readFile()
中,您正在读取尚未创建的文件
filename
,但
新文件(filename)
将在文件不存在时创建一个空文件。现在您正在读取一个空文件。将下面的代码放入您的
writeFile()

请注意不要使用
PrintWriter
PrintStreamWriter
,因为它们不会通过会导致问题的错误


您是否尝试在写入后关闭
PrintWriter
?您所说的工作不正常是什么意思?文件未创建,或者未调用
writeFile()
?告诉我们更多。它在什么方面不能正常工作?我看到的一件事是,尽管您已经在使用
println
@jackjay,但您在每个单词后面都添加了一个额外的
\n
,文件似乎没有创建,因为当我进入程序文件时,我看不到文件
PrintWriter
方法的问题是它们不会抛出任何错误。由于某些原因,该文件仍然没有显示在我的程序文件夹中。相同的代码在这里工作@coding_insancy,希望您已导入所需的文件。你能告诉我你的
文件名
变量包含什么吗?它现在起作用了,我只需要基本上刷新它。谢谢你的帮助,我真的很感激,是我的代码显示文件包含的错误,因为这似乎也不起作用
try{  
     FileWriter fw=new FileWriter(filename);  
     for (String words : arrayToWrite) {
        fw.write(words + "\n");
     } 
     fw.close(); 

   }catch(Exception e){
     System.out.println(e);
}