Hi All
I have a problem in .txt file. How can i delete some specific line from .txt file.
Plz help me
Printable View
Hi All
I have a problem in .txt file. How can i delete some specific line from .txt file.
Plz help me
Retrieve the content of that text file,
Remove the desired lines,
and save it again.
Thxs for reply
I am new in java.Can you give me more details.here is code.
So can you give me more about that..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();
}
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.
Code:FileWriter fw = new FileWriter("text.txt");
int x=0;
while(x<=arraylist.size()){
fw.append(arraylist.get(x));
x++;
}
fw.close();
ok can you give some code of delete the item...
first you try to your self then you ask for code..
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.Quote:
Can you give me some code becouse i am not getting
I am not geting plz give me some code..
What you have tried upto now?
What's the problem. Sukatoa already gives the code for you.
Can i show code
Yes, you can show your code.....
Here is code
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);
}
}
}
This is what sukatoa talking about.
Code: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());
}
}
}
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.