Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,234 people online right now. Registration is fast and FREE... Join Now!




problem with file

 
Reply to this topicStart new topic

problem with file, file problem

dato.java
17 Feb, 2008 - 03:59 AM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 30

here is code:
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 Feb, 2008 - 04:09 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Problem With File
17 Feb, 2008 - 04:17 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
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');

User is offlineProfile CardPM
+Quote Post

dato.java
RE: Problem With File
17 Feb, 2008 - 06:05 AM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 30

here is
code:
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?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Problem With File
17 Feb, 2008 - 06:49 AM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Problem With File
17 Feb, 2008 - 08:02 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
String.replaceAll

Please don't start several topics for the same question, and use code tags to show your code. code.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month