Results 1 to 4 of 4
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default 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:
    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();
    		
    	}
    	
    	
    }
    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:

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LennyKosmos View Post
    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...
    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.

    Quote Originally Posted by LennyKosmos View Post
    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:
    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

  3. #3
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LennyKosmos View Post
    Java Code:
    		Y.print("");
    You're welcome of course but there is no reason to print nothing.

    kind regards,

    Jos

Similar Threads

  1. Cannot find file for FileReader
    By Spooge in forum New To Java
    Replies: 11
    Last Post: 10-20-2010, 11:12 PM
  2. issue with FileReader and FileWriter
    By Uma R in forum Advanced Java
    Replies: 6
    Last Post: 07-16-2010, 01:16 PM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. FileWriter doesn't write
    By superman5 in forum NetBeans
    Replies: 2
    Last Post: 07-30-2009, 05:46 AM
  5. Replies: 5
    Last Post: 02-05-2009, 10:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •