open/edit file

I need to open a file, and edit part of the file based on some static text in that file. For example, lets say the text file looks something like this:

a

b

c

1

2

1

a

b

c

I need a program that will open the file, and delete everything from the first 1 to the last 1, and do nothing if no 1's exist.

Afterwards, save the existing file.

Can someone please show me how to do this?

Thanks.

[471 byte] By [ntsd2a] at [2007-11-27 9:10:18]
# 1
http://java.sun.com/docs/books/tutorial/essential/io/index.htmlIf you want more specific help, you'll have to ask a more specific question and show what you've done so far.
jverda at 2007-7-12 21:51:30 > top of Java-index,Java Essentials,Java Programming...
# 2

Here's a brief example of file reading and writing to get you started. Just pass in the name of a file (with a few lines of text in it) as a command line argument when you run this program

import java.io.*;

public class RandomAccessFileWriting

{

public static void main(String[]args) throws IOException

{

//put file to read/write as command line argument

RandomAccessFile raf = new RandomAccessFile(args[0], "rw");

//now I'll demonstrate some simple reading/writing, but not solve your problem completely

//It's funner that way

System.out.println("First two lines of file:");

System.out.println(raf.readLine());

System.out.println(raf.readLine());

System.out.println("Now I'll write a string to the third line");

raf.writeUTF("In Xanadu did Kubla Khan a stately pleasure-dome decree\n");

}

}

ericodea at 2007-7-12 21:51:30 > top of Java-index,Java Essentials,Java Programming...
# 3

I finished writing the program I was asking about. In case anyone needs anything like this in the future, I have posted the program here(w/ a few modifications):

import java.io.IOException;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.BufferedWriter;

import java.io.File;

import java.util.Collection;

import java.util.ArrayList;

public class ABC{

// Main method

public static void main(String args[]) {

// Stream to read file

FileReader fr;

FileWriter fw;

Collection<String> lineStorage = new ArrayList<String>();

String line = "";

boolean finishedRemoving = true;

final String filePath = "b.xml";

try {

// Open an input stream

fr = new FileReader(filePath);

// Read a line of text

BufferedReader br = new BufferedReader(fr);

while (br.ready()) { //while there's another line

line = br.readLine();

if (line.compareToIgnoreCase(" <app>") == 0) {

finishedRemoving = false;

continue;

} else if (line.compareToIgnoreCase(" </app>") == 0) {

finishedRemoving = true;

continue;

}

if (finishedRemoving) {

lineStorage.add(line);

}

}

// Close our input stream

fr.close();

fw = new FileWriter(filePath);

BufferedWriter bw = new BufferedWriter(fw);

for (String aLineStorage : lineStorage) {

bw.write(aLineStorage);

bw.newLine();

}

bw.flush();

fw.close();

}

// Catches any error conditions

catch (IOException e) {

System.err.println("Unable to read from file");

System.exit(-1);

}

}

}

ntsd2a at 2007-7-12 21:51:30 > top of Java-index,Java Essentials,Java Programming...
# 4

I copied the program you pasted on here and ran it, but it don't seem to work right for me. The way I'm looking at it is, it should replace everything that's within <app> and </app> with a new line. Please correct me if I'm wrong. I'd appreciate if you could you care to explain it a bit more in detail?

Like you posted initially, you were trying to erase the contents between the two 1s, i'm trying to replace the contents of a file with a new string. Thanks!

Message was edited by:

craz1zd

craz1zda at 2007-7-12 21:51:30 > top of Java-index,Java Essentials,Java Programming...