2015-06-27 35 views
2

由于某些原因,当我试图向称为newSession的文件写入一个称为持续时间的int整型并且程序编译完成时,我打开位于桌面上的文件,其他每个文件都是很好(意思是我想写入该文件的内容已成功),但newSession中写入了随机字母。为什么是这样的,任何人都可以解释为什么int的持续时间没有被写入文件newSession而是随机字母。BufferedWriter将随机字母写入文件(Java)

package kappa; 

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 

public class Reader 
{ 
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException 
    { 

     File dateFile = null; 
     Scanner reader = null; 

     try 
     { 
      String filePath = "/Users/john/Desktop/firstTime.txt"; 
      Scanner reader2 = null; 

      while(true) 
      { 
       FileWriter fw = new FileWriter(filePath); 
       BufferedWriter bw = new BufferedWriter(fw); 
       File firstTime = new File(filePath); 
       firstTime.createNewFile(); 
       bw.write("1"); 
       bw.close(); 
       reader2 = new Scanner(new File(filePath)); 
       break; 
      } 

      if(reader2.nextInt() == 1) 
      { 
       dateFile = new File("/Users/john/Desktop/Kunja.txt"); 
       dateFile.createNewFile(); 
       reader = new Scanner(dateFile); 
      } 

      // if file doesnt exists, then create it 
      if (dateFile.exists()) 
      { 
       FileWriter fw2 = new FileWriter(dateFile.getAbsoluteFile()); 
       BufferedWriter bw2 = new BufferedWriter(fw2); 

       dateFile.createNewFile(); 
       bw2.write("0"); 
       bw2.close(); 
       System.out.println("Done"); 

       int duration; 
       String ans = JOptionPane.showInputDialog ("Enter the amount of problems per training session (with number in minutes):");       

       while(!ans.matches("[0-9]+")) 
       { 
        ans = JOptionPane.showInputDialog ("Please re-enter the amount of problems per training session (with number in minutes):"); 
       }       

       duration = Integer.parseInt(ans); 
       System.out.println("Duration is " + duration); 

       int numSessions = (reader.nextInt() + 1); 
       System.out.println("Number of sessions is: " + numSessions); 
       String fileName = ("sessionNumber"+numSessions); 
       File newSession = new File("/Users/john/Desktop/"+fileName); 
       System.out.println(fileName); 

       if (!newSession.exists()) 
       { 
        newSession.createNewFile(); 
       }  

       FileWriter fw3 = new FileWriter(newSession.getAbsoluteFile()); 
       System.out.println("THE FILE PATH IS " + newSession.getAbsoluteFile()); 
       BufferedWriter bw3 = new BufferedWriter(fw3); 

       bw3.write(duration); 
       bw3.close(); 
      }else 
      { 
       int duration; 
       String ans = JOptionPane.showInputDialog ("Enter a number (only numbers please)");       

       while(!ans.matches("[0-9]+")) 
       { 
        ans = JOptionPane.showInputDialog ("Please re-enter a number (NOTHING ELSE!)"); 
       }       

       duration = Integer.parseInt(ans); 
       System.out.println(duration); 

       int numSessions = reader.nextInt(); 
       System.out.println("Number of sessions is: " + numSessions); 
       String fileName = ("sessionNumber"+numSessions); 
       File newSession = new File("/Users/john/Desktop/"+fileName); 
       System.out.println(fileName); 

       if (!newSession.exists()) 
       { 
        newSession.createNewFile(); 
        System.out.println("IT DOES NOT EXIST!"); 
       }  

       FileWriter fw = new FileWriter(newSession.getAbsoluteFile()); 
       BufferedWriter bw = new BufferedWriter(fw); 

       bw.write(duration); 
       bw.close(); 
      } 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
    } 
    } 
} 
+1

你写了二进制,而不是 '信'。读者和作家是文字。您应该使用OutputStream,否则在写入之前将该整数转换为文本。 – EJP

+0

SO缓冲作家只能写文字? – Kappa

+0

你在问我确认我刚才告诉你的。为什么? – EJP

回答

1

BufferedWriter#write(int)需要一个单个字符(由int表示)。如果要编写整数的文本表示形式,则必须自己将其转换为String

总之,替换:

bw3.write(duration); 

有了:

String durationString = String.valueOf(duration); 
bw3.write(durationString, 0, durationString.length());