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 8306133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:16:12+00:00 2026-06-08T18:16:12+00:00

I am a newbie and I really want to learn the concept instead of

  • 0

I am a newbie and I really want to learn the concept instead of just copying and pasting code. I wanted to learn how exactly to use the Jave IO and was confused and disapointed to see so my different versions of code.

So I made my own notes and wanted to confirm with the experts here whether I got it right. these are just for my own reference. I know is not perfect but I would appreciate if you could confirm whether they are correct.

Use BufferedWriter and FileWriter to write a text file ( as characters). Disadvantage is you cannot write a primitive data type.

Ex:

BufferedWriter bw= new BufferedWriter (new FileWriter("a.txt", true)); 
String x;
while ((x=bw.readLine())!=null){
bw.newLine();
bw.write(x);
bw.flush();}

Use BufferedReader and FileReader to read a text file (as characters)

Ex:

BufferedReader br = new BufferedReader (new FileReader("b.txt")); 
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}

Use DataOutputStream and FileOutputStream to write to a text file (in binary). Advantage is you can write primitive data types as well as strings.

Ex:

DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));
dos.writeInt(cityIdA);    // int cityIdA = 9897;         
dos.writeUTF(cityNameA); //  String cityNameA = "Green Lake City";  
dos.writeInt(cityPopulationA); //   int cityPopulationA = 500000;  
dos.writeFloat(cityTempA); //  float cityTempA = 15.50f; 
dos.flush();

Use DataInputStream and FileInputStream to read a text file (in binary). Advantage is you can read primitive data types as well as strings.

Ex:

DataInputStream dis = new DataInputStream(new FileInputStream("inp.txt"));
int cityId1 =dis.readInt();    // int cityIdA = 9897;         
String cityName1 =dis.readUTF(); //  String cityNameA = "Green Lake City";  
int cityPopulation1 =dis.readInt(); //   int cityPopulationA = 500000;  
float cityTemperature1 =dis.readFloat(); //  float cityTempA = 15.50f; 

Actual code:

import java.io.*; 

class b{

public static void main (String args[]) throws IOException{
int cityIdA = 9897;         
String cityNameA = "Green Lake City"; 
int cityPopulationA = 500000;         
float cityTempA = 15.50f;      

BufferedWriter bw = new BufferedWriter(new FileWriter("shahar.txt"));

bw.write("9897");  
bw.write("Green Lake City"); 
bw.write("500000"); 
bw.write("15.50"); 
bw.flush();             
bw.close(); 

DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));

dos.writeInt(cityIdA);      
dos.writeUTF(cityNameA);
dos.writeInt(cityPopulationA); 
dos.writeFloat(cityTempA);
dos.flush();

BufferedReader br = new BufferedReader (new FileReader("shahar.txt")); 
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}


DataInputStream dos1 = new DataInputStream(new FileInputStream("out.txt"));
int cityId1 = dos1.readInt();    // int cityIdA = 9897;     
System.out.println( cityId1);
String cityName1 =dos1.readUTF(); //  String cityNameA = "Green Lake City";  
System.out.println(cityName1);
int cityPopulation1 =dos1.readInt(); //   int cityPopulationA = 500000;  
System.out.println(cityPopulation1);
float cityTemperature1 =dos1.readFloat(); //  float cityTempA = 15.50f; 
System.out.println(cityTemperature1);
 }
 }
  • 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-06-08T18:16:13+00:00Added an answer on June 8, 2026 at 6:16 pm

    Your code subjectively has stylistic issues. I’ll drop your examples here as though I wrote them myself.

    Example 2

    (I put this before example 1 for a reason)

    BufferedReader br = new BufferedReader (new FileReader("b.txt")); 
    String x;
    
    while ((x = br.readLine()) != null)
    { // changed style, but otherwise fine
        System.out.println(x);
    }
    

    Example 1

    // borrowed reader from example 1
    BufferedReader br = new BufferedReader (new FileReader("b.txt")); 
    BufferedWriter bw = new BufferedWriter (new FileWriter("a.txt", true)); 
    String x;
    
    // this part didn't work.  You were trying to read from a writer, as you write to it.
    // if this were an object capable of reading and writing at the same time, you would
    // run into issues because they wouldn't be rewinding after each operation and you'd
    // end up with a combination of unexpected mirrored lines and garbled crap.
    
    // changed loop to read from reader.  reads data from one file, line by line, and
    // writes to another.
    while ((x = br.readLine()) != null) 
    {
        bw.write(x);  // in the other order, file will start with a blank line
        bw.newLine(); // in this order, file will end with one instead
    }
    bw.flush(); // its better if this is not in the loop
    

    Example 3 and 4

    Examples 3 and 4 are fine. You would use something like this if you needed to write lots of primitive types to a binary file. The other examples are much more suited for reading text.

    Since you’re a newbie, I’ll give you some random advice too:

    • Don’t blame a language for its shortcomings, learn to overcome them. (applies to all languages)
    • Threads are not a solution to every problem. If you need more than one, write yourself a scheduler.
    • There are often many ways to do the same thing, but often one is more right than the others.
    • Don’t write code now that you will hate yourself for later.
    • Which style you choose to use is irrelevant, but stick to it. That said, I don’t like yours. 🙂
    • Whichever style you choose, make sure you can read it easily under all conditions.
    • Your computer does not have infinite memory, don’t write code pretending that it does. The same goes for all other resources; nobody likes a glutton.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just want to mention that I am really a newbie in API development (concepts,
I want to start to do some unit testing however I am really confused
I'm a newbie in R so, I really need some help here. I just
I am a NodeJS newbie and have a really simple question. I want to
Sorry for this lame question, but I really am a database newbie: I want
I am really newbie at RegExp's and I can't build it right now, so
A newbie question,really. Suppose I have a html table like this: <div id=div1> <table
This is probably a really newbie question (well, I'm pretty sure it is), but
A really php newbie here, sorry if the question is too simple. I’m following
Im really really a newbie in regexp and I can’t figure out how to

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.