|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-20-2008, 03:14 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
|
Delete From .txt file
Hi All
I have a problem in .txt file. How can i delete some specific line from .txt file.
Plz help me
|
|

06-20-2008, 03:37 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
Retrieve the content of that text file,
Remove the desired lines,
and save it again.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-20-2008, 03:45 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Thxs for reply
I am new in java.Can you give me more details.here is code.
try {
String str ="xeee.txt";
File f = new File(str);
if(f.exists()){
BufferedWriter out = new BufferedWriter(new FileWriter("xeee.txt",true));
if(ADDSERVER.getText().equals("")){
ADDSERVER.requestFocus();
}
else{
out.write(ADDSERVER.getText());
if(f.canWrite()){
// text.setText("");
ADDSERVER.setText("");
out.newLine();
//area.append("\n");
// text.requestFocus();
}
else{
}
out.close();
}
}
else{
text.setText("");
//text.requestFocus();
}
}
catch(Exception x){
x.printStackTrace();
}
So can you give me more about that..
|
|

06-20-2008, 04:01 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
That kind of problem needs a sample input and expected output to be analyze... not the code.
Here is an algo,
Since removing such line is the goal,
Use Scanner class....
in every line that will be encountered, save it on an array(I prefer to use ArrayList for this)
When removing such text on that specific line,
use remove(int index) method.
Example:
If you have choosen to remove the 7th line,
remove(6);
Since the offset of the ArrayList is zero, you have to subtract the prefered line by one.
Then save them all again using FileWriter.
FileWriter fw = new FileWriter("text.txt");
int x=0;
while(x<=arraylist.size()){
fw.append(arraylist.get(x));
x++;
}
fw.close();
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by sukatoa : 06-20-2008 at 04:07 PM.
Reason: forgot to add variable x to increment
|
|

06-20-2008, 04:13 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
|
ok can you give some code of delete the item...
Last edited by Sarinam : 06-20-2008 at 04:20 PM.
|
|

06-20-2008, 04:41 PM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
first you try to your self then you ask for code..
Last edited by Mir : 06-20-2008 at 04:48 PM.
|
|

06-21-2008, 12:26 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
|
Can you give me some code becouse i am not getting
|
|

06-21-2008, 02:31 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 898
|
|
Can you give me some code becouse i am not getting
Hey, as mentioned above, first you try and show us what you've done. You know, the more effort you show, often the more help you receive here.
|
|

06-23-2008, 01:26 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
|
I am not geting plz give me some code..
|
|

06-23-2008, 01:30 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
What you have tried upto now?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 01:41 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Originally Posted by Eranga
What you have tried upto now?
i write and store these valuse in array list.And now i am not abel to delete it.If i got some line of code then i will do it..
|
|

06-23-2008, 01:44 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
What's the problem. Sukatoa already gives the code for you.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 01:51 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Originally Posted by Eranga
What's the problem. Sukatoa already gives the code for you.
Hi Eranga
it is not working..
|
|

06-23-2008, 02:02 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
|
Can i show code
|
|

06-23-2008, 02:09 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
Yes, you can show your code.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 02:15 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Originally Posted by sukatoa
Yes, you can show your code.....
Here is code
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class dlo extends Dialog
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Frame parent;
private Button OK;
private Button CANCEL;
private List SERVERS;
private TextField ADDSERVER;
private Button ADD;
private Button DELETE;
private ArrayList<String> ars;
private int pop;
private ArrayList<String> arip = new ArrayList<String>();
private String strLine;
public dlo (Frame parent)
{
super(parent, "Ik", true);
OK = new Button("OK");
CANCEL = new Button("CANCEL");
SERVERS = new List(10, true);
ADDSERVER = new TextField(15);
ADD = new Button("< Add");
DELETE = new Button("Delete");
setLayout(new GridBagLayout());
addToBag(this, SERVERS, 0, 0, 1, 3, 1.0,0.0);
addToBag(this, ADD, 1, 0, 1, 1, 0.0,0.0);
addToBag(this, ADDSERVER, 2, 0, 2, 1, 0.0,0.0);
addToBag(this, DELETE, 1, 1, 1, 1, 0.0,0.0);
addToBag(this, CANCEL, 2, 4, 1, 1, 0.0,0.0);
addToBag(this, OK, 3, 4, 1, 1, 0.0,0.0);
pack();
Point location = parent.location();
Point fatherLocation = location;
move(fatherLocation.x + 100,fatherLocation.y + 100);
show();
}
private void addToBag(Container container, Object arg, int x, int y, int w, int h,
double weightx, double weighty)
{
GridBagLayout gbl = (GridBagLayout)container.getLayout();
GridBagConstraints c = new GridBagConstraints();
Component component;
c.fill = GridBagConstraints.BOTH;
c.gridx = x;
c.gridy = y;
c.gridwidth = w;
c.gridheight = h;
c.weightx = weightx;
c.weighty = weighty;
component = (Component)arg;
container.add(component);
gbl.setConstraints(component, c);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof Button) {
if (evt.target == OK) {
return updateDB();
}
else if (evt.target == CANCEL) {
hide();
dispose();
return true;
}
else if (evt.target == ADD) {
if (ADDSERVER.getText() != "")
SERVERS.addItem(ADDSERVER.getText());
try {
String str ="xeee.txt";
File f = new File(str);
if(f.exists()){
// BufferedWriter out = new BufferedWriter(new FileWriter("xeee.txt",true));
FileWriter fstream = new FileWriter("xeee.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
// int x=0;
// while(x<=arip.size()){
// fstream.append(arip.get(x));
// x++;
// }
if(ADDSERVER.getText().equals("")){
int x=0;
while(x<=ars.size()){
SERVERS.addItem(ars.get(x));
x++;
}
SERVERS.requestFocus();
}
else{
out.write(ADDSERVER.getText());
if(f.canWrite()){
ADDSERVER.setText("");
out.newLine();
}
else{
}
}
}
}
catch(Exception x){
x.printStackTrace();
}
}
return true;
}
else if (evt.target == DELETE) {
try{
FileWriter fstream1= new FileWriter("xeee.txt", true);
FileInputStream fstrea = new FileInputStream("xeee.txt");
DataInputStream in = new DataInputStream(fstrea);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null) {
arip.add(strLine);
JOptionPane.showMessageDialog(null,arip);
}
int x=0;
while(x<=arip.size()){
fstream1.append(arip.get(x));
x++;
}
in.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
return true;
}
return false;
}
private boolean updateDB()
{
hide();
dispose();
return true;
}
public boolean handleEvent (Event event)
{
switch (event.id) {
case Event.WINDOW_DESTROY:
dispose();
return true;
default:
return super.handleEvent(event);
}
}
}
|
|

06-23-2008, 02:20 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
This is what sukatoa talking about.
import java.io.*;
import java.util.*;
/**
*
* @author Eranga Tennakoon
*/
public class RemoveFileContent {
private ArrayList<String> data;
private Scanner dataScan;
public RemoveFileContent() {
data = new ArrayList<String>();
}
public static void main(String[] args) {
new RemoveFileContent().readFile();
}
private void readFile() {
FileInputStream fis = null;
try {
fis = new FileInputStream("files/EditedFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
data.add(line);
}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
finally {
try {
fis.close();
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
removeData();
}
private void removeData() {
dataScan = new Scanner(System.in);
System.out.println("Which line you want to remove: ");
int i = Integer.parseInt(dataScan.nextLine());
data.remove(i - 1);
writeDataBack();
}
private void writeDataBack() {
FileWriter fw = null;
int x = 0;
try{
fw = new FileWriter("files/DataToFile.txt");
BufferedWriter out = new BufferedWriter(fw);
while(x < data.size()) {
out.write(data.get(x));
out.newLine();
x++;
}
out.close();
}
catch(IOException ioex){
System.out.println(ioex.getLocalizedMessage());
}
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-24-2008, 08:13 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Originally Posted by Eranga
This is what sukatoa talking about.
import java.io.*;
import java.util.*;
/**
*
* @author Eranga Tennakoon
*/
public class RemoveFileContent {
private ArrayList<String> data;
private Scanner dataScan;
public RemoveFileContent() {
data = new ArrayList<String>();
}
public static void main(String[] args) {
new RemoveFileContent().readFile();
}
private void readFile() {
FileInputStream fis = null;
try {
fis = new FileInputStream("files/EditedFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
data.add(line);
}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
finally {
try {
fis.close();
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
removeData();
}
private void removeData() {
dataScan = new Scanner(System.in);
System.out.println("Which line you want to remove: ");
int i = Integer.parseInt(dataScan.nextLine());
data.remove(i - 1);
writeDataBack();
}
private void writeDataBack() {
FileWriter fw = null;
int x = 0;
try{
fw = new FileWriter("files/DataToFile.txt");
BufferedWriter out = new BufferedWriter(fw);
while(x < data.size()) {
out.write(data.get(x));
out.newLine();
x++;
}
out.close();
}
catch(IOException ioex){
System.out.println(ioex.getLocalizedMessage());
}
}
}
it help me aloat to understand but i want to delete some specific test through the texarea input..
Can you give me concept of it..
|
|

06-24-2008, 08:17 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
Display all read line to a text area first, at the same time keep them recorded in ArrayList. Then remove the selected line from the text are and refer the selected item index to remove from the List.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 08:05 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 121
|
|
Originally Posted by Eranga
Display all read line to a text area first, at the same time keep them recorded in ArrayList. Then remove the selected line from the text are and refer the selected item index to remove from the List.
Sir i am abel to store all record to ArrayList.But to display in textarea How can i do sir.i jave no idia aboput that.Plz help me
|
|
|