Results 1 to 4 of 4
- 10-27-2010, 07:35 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
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:
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!Java 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(); } }
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:
- 10-27-2010, 07:46 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Do you want to append to an existing file, e.g. when you have file A B C and want to append a D you want your new file to be A B C D? If so, read the API documentaton the FileWriter (or FileOutputStream) class; it has a constructor that creates one that appends to an already existing file.
Programming is not sculpturing; a program has to do what it has to do; everything you take away from it is more like an amputation ;-)
kind regards,
Jos
- 10-27-2010, 08:09 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
oh yeah
public void läggtillkund() throws IOException{
FileWriter x = new FileWriter(Kundfil, true);
PrintWriter Y = new PrintWriter(x);
Y.print("");
Y.println("xxxx");
Y.close();
}
fixed, thanks alot josah <3
- 10-27-2010, 08:12 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Cannot find file for FileReader
By Spooge in forum New To JavaReplies: 11Last Post: 10-20-2010, 11:12 PM -
issue with FileReader and FileWriter
By Uma R in forum Advanced JavaReplies: 6Last Post: 07-16-2010, 01:16 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
FileWriter doesn't write
By superman5 in forum NetBeansReplies: 2Last Post: 07-30-2009, 05:46 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks