write to file with filereader/filewriter
so basically what i want to do is add data(variables namn, efternamn and personnumer, which means name, lastname and social security number) to a file.. i managed to update a file but i just want to add a a person to this file, and nothing else... heres what i got so far:
Code:
import java.io.*;
import javax.swing.*;
public class Kund {
private String namn, efternamn, personnummer;
private String Kundfil="Kunder.txt";
private String temp="tmp.txt";
public void uppdatera(String angenamn)throws IOException{
[B]FileReader X = new FileReader(Kundfil);[/B]
FileWriter Y = new FileWriter(temp);
BufferedReader input = new BufferedReader(X);
PrintWriter stream = new PrintWriter(Y);
[B]while((namn=input.readLine())!=null){
efternamn = input.readLine();
personnummer = input.readLine();[/B]
if(namn.equals(angenamn)){
namn=JOptionPane.showInputDialog("Ange nytt namn");
efternamn=JOptionPane.showInputDialog("Ange nytt efternamn");
personnummer=JOptionPane.showInputDialog("Ange nytt personnummer");
}
stream.println(namn);
stream.println(efternamn);
stream.println(personnummer);
}
input.close();
stream.close();
kopierafiler(temp, Kundfil);
File f=new File(temp);
f.delete();
}
public void kopierafiler(String fileIn, String fileOut) throws IOException{
FileReader kopieraFR = new FileReader(fileIn);
FileWriter kopieraFW = new FileWriter(fileOut);
BufferedReader inputStream = new BufferedReader(kopieraFR);
PrintWriter outputStream = new PrintWriter(kopieraFW);
String rad;
while ((rad = inputStream.readLine()) != null) {
outputStream.println(rad);
}
inputStream.close();
outputStream.close();
}
public static void main (String[] args)throws IOException{
String angenamn;
Kund X = new Kund();
angenamn=JOptionPane.showInputDialog("ange namn");
X.läggtillkund();
//new Order();
}
}
thanks in advance guys, this forum has helped me tremendously and i hope i can give it back some day.. but for now, im learning!
i suspect that the bold parts isnt required for writing to a file? i dont dare to remove any more code, ive tried, and it went bad :confused: