Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:16:24+00:00 2026-05-26T06:16:24+00:00

EDIT This is my file reader, can I make this read it from bottom

  • 0

EDIT
This is my file reader, can I make this read it from bottom to up seeing how difficult it is to make it write from bottom to up.

        BufferedReader mainChat = new BufferedReader(new FileReader("./messages/messages.txt"));
    String str;
    while ((str = mainChat.readLine()) != null) 
    {
        System.out.println(str);
    }
    mainChat.close();

OR (old question)

How can I make it put the next String at the beginning of the file and then insert an new line(to shift the other lines down)?

FileWriter chatBuffer = new FileWriter("./messages/messages.txt",true);
BufferedWriter mainChat = new BufferedWriter(chatBuffer);
mainChat.write(message);
mainChat.newLine();
mainChat.flush();
mainChat.close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:16:25+00:00Added an answer on May 26, 2026 at 6:16 am

    Someone could correct me, but I’m pretty sure in most operating systems, there is no option but to read the whole file in, then write it back again.

    I suppose the main reason is that, in most modern OSs, all files on the disc start at the beginning of a boundary. The problem is, you cannot tell the file allocation table that your file starts earlier than that point.

    Therefore, all the later bytes in the file have to be rewritten. I don’t know of any OS routines that do this in one step.

    So, I would use a BufferedReader to store whole file into a Vector or StringBuffer, then write it all back with the prepended string first.

    —

    Edit

    A way that would save memory for larger files, reading @Saury’s randomaccessfile suggestion, would be:

    file has N bytes to start with
    we want to add on "hello world"
    open the file for append
    append 11 spaces
    i=N
    loop {
       go back to byte i
       read a byte 
       move to byte i+11
       write that byte back
       i--
    } until i==0
    then move to byte 0
    write "hello world" 
    

    voila

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method to get xml file from google reader with the contens
I have been told to edit this file in Sharepoint Designer: /_layouts/KWizCom_WikiPlus/CreateNew.aspx I found
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i
I am making a site that consumes a csv file this file can come
I have a htm file can i read it as UTF-8 formatted file without
My program basically looks like this: using(XmlReader reader = XmlReader.Create(File.Open(path, FileMode.Open, FileAccess.Reader, FileShare.Read)) {
I have to edit the contents of a file and write the edited conted
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.