Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Set the position of the caret in a File

Posted on 2006-11-28
23
Medium Priority
?
233 Views
Last Modified: 2010-03-31
Hi,
simple question,
I want to write an existing file, in a specific location,
for example :

                  BufferedWriter out =
                        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true),"8859_1"));
                  out.newLine();
                  out.write("Hello World");

Hiow can I choose the location of the "Hello World" in the file ?
Thank u.

0
Comment
Question by:matthew016
  • 11
  • 8
  • 4
23 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 18026373
Use RandomAccessFile -> seek()

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18026387
You can't do this with buffered output, because the buffer has its own
strategy to keep things in memory.

Just create a File, then a RandomAccessFile(File, "rw");
then seek the position you like and write (may be overwrite).
Consider such a file as a byte/character array on disk.

;JOOP!
0
 
LVL 9

Author Comment

by:matthew016
ID: 18026560
Thanks, what's the equivalent of :

out.newLine();

with the random access file ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:matthew016
ID: 18026568
Also, I do not to specify encoding ?
iso, utf-8, ...

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18026633
1) write((byte)'\n');
2) you got to do the coding yourself; you could write to a String array or byte array
and write that into the RandomAccessFile.

Btw.: this is a bad way to update text files.

;JOOP!
0
 
LVL 9

Author Comment

by:matthew016
ID: 18026760


I want to insert this :

<!DOCTYPE configuration [
  <!ENTITY amp "&#x26;">
  <!ENTITY lt "&#x3C;">
  <!ENTITY minus "&#45;">
]>

into XML files, it must be just after the first line : <?xml version="1.0" encoding="ISO-8859-1"?>

                  File f = new File(path);
                  RandomAccessFile raf = new RandomAccessFile(f, "rw");

                  raf.seek(45);
   
                  raf.writeChars("<!DOCTYPE configuration [");
                  raf.writeChar((byte)'\n');
                  raf.writeChars("<!ENTITY amp \"&#x26;\">");
                  raf.writeChar((byte)'\n');
                  raf.writeChars("<!ENTITY lt \"&#x3C;\">");
                  raf.writeChar((byte)'\n');
                  raf.writeChars("<!ENTITY minus \"&#45;\">");
                  raf.writeChar((byte)'\n');
                  raf.writeChars("]>");
                  raf.writeChar((byte)'\n');
                  raf.close();

What is a bad way ?
I didn't follow u.

0
 
LVL 24

Accepted Solution

by:
sciuriware earned 1000 total points
ID: 18026811
              RandomAccessFile raf = new RandomAccessFile(new File(path), "rw");
               raf.seek(45);
               raf.writeChars("<!DOCTYPE configuration [\n<!ENTITY amp \"&#x26;\">\n<!ENTITY lt \"&#x3C;\">\n<!ENTITY minus \"&#45;\">\n]>\n");
               raf.close();
bad:
      well I did a lot of text (even source) updating, but I always read the entire text into a StringBuffer
      modified it and dumped it back.
You write 98 characters into that xml file. Mmmm ..., what's after that 98th character?
Keep on the safe side and don't try to save nano seconds, protect the integrety of your files.

;JOOP!

P.S.: I invested some time long ago in an Editor class that reads, edits and saves files just like an editor.
My best investment in software ever.

;!
0
 
LVL 9

Author Comment

by:matthew016
ID: 18027056
Sorry, I don't understand wh
0
 
LVL 9

Author Comment

by:matthew016
ID: 18027058
*at u are saying,
why do u ask me what's after the 98th car ?
0
 
LVL 9

Author Comment

by:matthew016
ID: 18027080
it doesn't work,
It writes a lot of special carachters instead of the text
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18027168
Believe me, what you are doing is wrong:
when a text within a textfile should be changed, then read the file, make the change and write it back.
This counts for files up to 10Mb.

And about character 98:

initial file:      |||||||||||||||||||pos 45||||||||||||||||||||||||||||||||||||||||||pos45+97|whatishere?||||||||||||||||||||||
if you succeed with your approach:
modified file: |||||||||||||||||||pos 45|changed data||||||||||||||||||||||||||||pos45+97|whatishere?||||||||||||||||||||||

Now, does the end of your text seamlessly connect to the data at pos 45+98?
And will it in the future? XML files may change in structure.

;JOOP!
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 1000 total points
ID: 18027205
Using a StringBuffer would be easier.

try
        {
            BufferedReader in = new BufferedReader(new FileReader("<your file>"));
            StringBuffer sb = new StringBuffer();
            String line = in.readLine();
            sb.append(line).append("<!DOCTYPE configuration [\n").append("<!ENTITY amp \"&#x26;\">\n").append("<!ENTITY lt \"&#x3C;\">\n").append("<!ENTITY minus \"&#45;\">\n").append("]>\"\n)");
            while ((line = in.readLine()) != null)
            {
                sb.append(line);
            }
            in.close();
           
            BufferedWriter bw = new BufferedWriter(new FileWriter("<your file>"));
            bw.write(sb.toString());
            bw.flush();
            bw.close();
           
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18027225
That's what I said.

;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 18027232
I didn't say you didn't.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18027254
I didn't say you said I didn't ...

OK we agree, the questioners approach is wrong.
Yours is a good solution, which should be accepted and followed.

;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 18027282
I just provided some implementation code of how it can be done using a StringBuffer. The suggestion is yours. If the asker finds it useful I think a split would be fairer.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18027324
I'm already happy when all those random-write-in-text-programmers would stop.
There's lots of applications that will waste their files in the near future; time-bombs!
matthew016, do you understand?
Today the 'field' that you want to alter may be 98 characters long.
What will it be next week? How will they find out?
;JOOP!
0
 
LVL 9

Author Comment

by:matthew016
ID: 18027675
Yep I understand ebcause I hardcode the number of cars after which it should append .

But the StringBuffer is the same thing ?
who says that we must write the doctype after the first line ?

Same problem occur here.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18028278
For your information, I use StringBuffer on java sources.
When I update certain line-parts (e.g. copyright notices, version strings) I do a context search.
It never fails.
If you blindly replace some characters without any check ............................. hope you don't work with guided missiles.

;JOOP!
0
 
LVL 9

Author Comment

by:matthew016
ID: 18028508
How do u know I'm working on guided missiles  ?!  ^_-
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18029411
I guessed.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18029534
:)
0
 
LVL 35

Expert Comment

by:girionis
ID: 18034970
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question