-
Nov 23rd, 2011, 02:13 AM
#1
Thread Starter
Hyperactive Member
[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...
-
Nov 26th, 2011, 01:36 PM
#2
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, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 27th, 2011, 08:13 AM
#3
Thread Starter
Hyperactive Member
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();
}
}
-
Nov 27th, 2011, 09:39 AM
#4
Thread Starter
Hyperactive Member
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();
}
}
-
Dec 26th, 2011, 01:26 AM
#5
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!
.
-
Dec 26th, 2011, 11:23 AM
#6
Thread Starter
Hyperactive Member
Re: file handling
Originally Posted by honeybee
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|