I recently posted some code and tested the code with a .txt file. now that I have the code working my task is to change the .txt to a .dat file, now my code does not write to the file. Am I missing something or do something wrong, I need some help and answers to this. Can anyone help me? I have enclosed the code if anyone needs to look at it to figure out why it doesn't work with a .dat file.
import java.util.*;
import java.io.*;
import java.lang.*;
public class StringtoFile {
String file=new String("UserStrings.dat");
FileWriter fileStream = null ;
{try {
fileStream = new FileWriter(file);
}
catch (IOException e) {
System.out.println("there was an Error");
}
}
public static void main(String[] args) throws IOException
{
StringtoFile h = new StringtoFile ();
h.openFile();
h.addline();
h.closefile();
}
private Scanner scan, X;
public void openFile()
{
X = new Scanner(file);
}
public void addline(){
String str="initial";
System.out.println("Enter a Sentence.....");
while(!str.equalsIgnoreCase("done")) {
// System.out.println("Enter a Sentence.....");
Scanner in=new Scanner(System.in);
str = in.nextLine();
try {
if(!str.equalsIgnoreCase("done"))
fileStream.write(str+"\n");
}
catch (IOException e) {
System.out.println("There was an Error");
}
}
}
public void closefile(){
X.close();
}
}