简体   繁体   English

不会写入文件吗?

[英]Won't write to file?

So, I'm trying to create this app, and am still in the beginning, but every time i run it and try to write in the file Dictionary.txt, the file will be completely empty. 因此,我正在尝试创建此应用,但仍处于起步阶段,但是每次我运行它并尝试在Dictionary.txt文件中写入文件时,该文件将完全为空。 It would work in this other thing I've made, but here it just runs well, gets terminated, and when I open the file it is blank, unwritten. 它可以在我做的另一件事中起作用,但是在这里它运行得很好,被终止了,当我打开文件时,它是空白的,未写入。 Currently on Eclipse Mars, and it gives me no errors (apart from the random object not being used, but I plan to use it later on.) 当前在Eclipse Mars上运行,它没有给我带来任何错误(除了未使用随机对象外,我计划稍后再使用。)

package mainPackage;

import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;

public class MainClass {

    private static FileWriter fw;
    private static BufferedWriter out;
    private static Scanner input;
    private static Scanner file;
    private static Random slc;

    public static void main(String[] args) throws IOException{

        fw = new FileWriter("C:/Users/ALAN_BARE/Desktop/Dictionary.txt" , true );
        input = new Scanner(System.in);
        file = new Scanner(new File("C:/Users/ALAN_BARE/Desktop/Dictionary.txt"));
        out = new BufferedWriter(fw);
        slc = new Random();

        String engword;
        String gerword;
        String response;

        System.out.println("What do you want to do, register words or play the game?");
        response = input.nextLine();

        if(response.contains("register")){
            System.out.println("Type yor english word:");
            engword = input.nextLine();
            System.out.println("Type the translation in German:");
            gerword = input.nextLine();
            out.write(engword);
            out.write(gerword);

            if(file.hasNextLine()){
                out.newLine();
                out.newLine();
                out.write(engword);
                out.write(" " + gerword);
            } else{
                out.write(engword);
                out.write(" " + gerword);
            }
        } else if(response.contains("Register")){

            System.out.println("Type yor english word:");
            engword = input.next();
            System.out.println("Type the translation in German:");
            gerword = input.next();

            if(file.hasNextLine()){
                out.newLine();
                out.newLine();
                out.write(engword);
                out.write(" " + gerword);

            } else{
                out.write(engword);
                out.write(" " + gerword);
            }
        }
    }
}

close the BufferedWriter. 关闭BufferedWriter。 out.close()

Suggestion: 建议:

Put below code out of if-else block, as these are common 将以下代码放在if-else块之外,因为它们很常见

out.write(engword);
out.write(" " + gerword);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM