2016-07-01 3 views
0

Итак, я пытаюсь создать это приложение и все еще в начале, но каждый раз, когда я его запускаю и пытаюсь записать в файл Dictionary.txt, файл будет полностью пустым. Это будет работать в этом другом, что я сделал, но здесь он работает хорошо, заканчивается, и когда я открываю файл, он пуст, неписан. В настоящее время на Eclipse, Марс, и это не дает мне никаких ошибок (кроме случайного объекта не используется, но я планирую использовать его в дальнейшем.)Не записывать в файл?

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); 
      } 
     } 
    } 
} 
+2

'близко()' ресурсы. 'Out.close()' – Kartic

ответ

2

закрыть BufferedWriter. out.close()

Предложение:

Put ниже код из если-иначе блок, так как они являются общими

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