[RESOLVED] file handling-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] file handling

  1. #1

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Resolved [RESOLVED] file handling

    hi all,
    currently i'm working on some program, i know about file reading, writing so as new to java i want to do some more practice,
    so i want to update record of a person that is stored in text file, can anyone help me to start this?? how would i do this? (i'm not asking for code)






    thanks in advance...

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,572

    Re: file handling

    How this records are represented in text file ? Comma separated text ?


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India

    Skills: PHP, MySQL, jQuery, VB.Net, VB6, Photoshop...

  3. #3

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: file handling

    i'm doing something like this:
    Code:
    import java.util.*;
    import java.io.*;
    import java.io.IOException;
    
    
    
    public class Student{
    private String name;
    private int rollNo;
    public static int num=0;
    public void setName(String n)
    	{
    		name=n;
    	}
    String getName()
    	{
    		return name;
    	}
    public void setRoll(int n)
    	{
    			rollNo=n;
    	}
    int getRoll()
    	{
    			return rollNo;
    	}
    public Student(String n, int rollno)
    	{
    		name=n;
    		rollNo=rollno;
    	}
    public Student()
    	{}
    public String toString()
        {
           return (name+rollNo);
    	}
    
      public static void main(String[] args)throws IOException {
      String cname;
      int rollno;
      Student c=new Student();
      Student c1=new Student();
      InputStreamReader isr = new InputStreamReader( System.in );
      BufferedReader br = new BufferedReader( isr );
    
    
    	System.out.println( "Enter your name : " );
          cname = br.readLine( );
    	  c.setName(cname);
    	  cname=c.getName();
    	System.out.println( "Enter roll : " );
    	  rollno = Integer.parseInt(br.readLine( ));
    	  c.setRoll(rollno);
    	  rollno=c.getRoll();
    	  Student i1=new Student(cname, rollno);
    
    	  System.out.println( "Enter your name : " );
          cname = br.readLine( );
    	  c1.setName(cname);
    	  cname=c1.getName();
    	  System.out.println( "Enter roll : " );
    	  rollno = Integer.parseInt(br.readLine( ));
    	  c.setRoll(rollno);
    	  rollno=c.getRoll();
    	  Student i2=new Student(cname, rollno);
      ArrayList<Student> arl=new ArrayList<Student>();
    
      arl.add(i1);
      arl.add(i2);
    
      System.out.println("The content of arraylist is: " + arl);
    
    
      FileWriter fw = new FileWriter( "Ayesha.txt", true );
      BufferedWriter bw = new BufferedWriter( fw );
    				bw.write(arl.toString());
    				bw.close();
    
    
      }
    }

  4. #4

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: file handling

    i did this now, but it's changing every where in file..
    any ideas about restrict to change at particular value of arrayList?
    can you please check this one too?
    this one is updated code.
    Code:
    import java.util.*;
    import java.io.*;
    import java.io.IOException;
    
    
    
    public class Student{
    private String name;
    private int rollNo;
    public static int num=0;
    public void setName(String n)
    	{
    		name=n;
    	}
    String getName()
    	{
    		return name;
    	}
    public void setRoll(int n)
    	{
    			rollNo=n;
    	}
    int getRoll()
    	{
    			return rollNo;
    	}
    public Student(String n, int rollno)
    	{
    		name=n;
    		rollNo=rollno;
    	}
    public Student()
    	{}
    public String toString()
        {
           return (name+rollNo);
    	}
    
      public static void main(String[] args)throws IOException {
      String cname;
      int rollno;
      Student c=new Student();
      Student c1=new Student();
      InputStreamReader isr = new InputStreamReader( System.in );
      BufferedReader br = new BufferedReader( isr );
    
    
    	System.out.println( "Enter your name : " );
          cname = br.readLine( );
    	  c.setName(cname);
    	  cname=c.getName();
    	System.out.println( "Enter roll : " );
    	  rollno = Integer.parseInt(br.readLine( ));
    	  c.setRoll(rollno);
    	  rollno=c.getRoll();
    	  Student i1=new Student(cname, rollno);
    
    	  System.out.println( "Enter your name : " );
          cname = br.readLine( );
    	  c1.setName(cname);
    	  cname=c1.getName();
    	  System.out.println( "Enter roll : " );
    	  rollno = Integer.parseInt(br.readLine( ));
    	  c.setRoll(rollno);
    	  rollno=c.getRoll();
    	  Student i2=new Student(cname, rollno);
      ArrayList<Student> arl=new ArrayList<Student>();
    
      arl.add(i1);
      arl.add(i2);
    
      System.out.println("The content of arraylist is: " + arl);
    
    
      FileWriter fw = new FileWriter( "Ayesha.txt", true );
      BufferedWriter bw = new BufferedWriter( fw );
    				bw.write(arl.toString());
    				bw.close();
    
    
    /**********************update************************/
    
    
                 File file = new File("Ayesha.txt");
                 BufferedReader reader = new BufferedReader(new FileReader(file));
                 String line = "", oldtext = "";
                  Scanner sc=new Scanner(System.in);
    			 String newline=sc.nextLine();
    
    			 String oldline=sc.nextLine();
                 while((line = reader.readLine()) != null)
                     {
                     oldtext += line + "\r\n";
                 }
                 reader.close();
    
    			 String newtext = oldtext.replaceAll(oldline,newline );
    
                 FileWriter writer = new FileWriter("Ayesha.txt");
                 writer.write(newtext);writer.close();
    
    
      }
    }

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,334

    Re: file handling

    Why not store the contents of the text file back into the arrays first? You could then loop through the array and perform a better search/replace.

    Don't ask me for the code, as I don't work with Java and am now getting poor at writing code that's syntactically correct. Thank God I have MS with me!

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: file handling

    Quote Originally Posted by honeybee View Post
    Why not store the contents of the text file back into the arrays first? You could then loop through the array and perform a better search/replace.

    Don't ask me for the code, as I don't work with Java and am now getting poor at writing code that's syntactically correct. Thank God I have MS with me!

    .
    i was almost forgotten about this thread,
    now i won't do it with flat files, i'm doing it with database.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.