4 Replies - 651 Views - Last Post: 17 February 2008 - 09:02 AM Rate Topic: -----

#1 Guest_dato.java*


Reputation:

problem with file

Post icon  Posted 17 February 2008 - 04:59 AM

here is code:
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		try {
			File f = new File("JAVA.txt");
			FileWriter fw = new FileWriter(f);
			fw.write('a');
			fw.write('b');
			fw.write('c');
			fw.close();
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
}

~edit: code tags PB
what i have to do/write in this code if i want to remove from file symbol a?
if anyone know , please help me,thanks

This post has been edited by PennyBoki: 17 February 2008 - 05:09 AM


Is This A Good Question/Topic? 0

Replies To: problem with file

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: problem with file

Posted 17 February 2008 - 05:17 AM

You are writing a file here, so the easiest way to remove it is not to write it into it. So delete the line: fw.write('a');
Was This Post Helpful? 0
  • +
  • -

#3 Guest_dato.java*


Reputation:

Re: problem with file

Post icon  Posted 17 February 2008 - 07:05 AM

here is
code:
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		try {
			
			FileReader fr = new FileReader("C:\\java.txt");
			int a;
		 while((a=fr.read())!=-1) {
			  System.out.println((char)a);
}
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
}


what i have to add/change in this code if i want to remove some elements from this file?
Was This Post Helpful? 0

#4 GWatt  Icon User is online

  • member icon

Reputation: 269
  • View blog
  • Posts: 3,067
  • Joined: 01-December 05

Re: problem with file

Posted 17 February 2008 - 07:49 AM

You could read the file into a String object, and then use .substring() to cut out the chunks you don't want. Or use "+=" to add stuff.
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: problem with file

Posted 17 February 2008 - 09:02 AM

String.replaceAll

Please don't start several topics for the same question, and use code tags to show your code. :code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1