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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:59:03+00:00 2026-06-17T10:59:03+00:00

I’m learning File I/O using Java. Following are my codes from two different Java

  • 0

I’m learning File I/O using Java.

Following are my codes from two different Java files. One is “File” with the main class, the other is “FileWrite.”

I was able to implement string input and output. But the output textfile has gibberish in the beginning and I am not sure why.

[File.Java]

package file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class File {
    public static void main(String[] args) {

    try (BufferedReader br = new BufferedReader(new FileReader("B:\\fileIn.txt")))
            {
              String stCurrent;

              while ((stCurrent = br.readLine()) != null) {
                    System.out.println(stCurrent);
            }

            } catch (IOException e) {
                e.printStackTrace();
            } 
                    FileWrite fW = new FileWrite();
                    fW.serializeAddress("Boston", "Canada");
        }
}

[FileWrite.Java]

package file;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class FileWrite {

   public void serializeAddress(String city, String country) {
       try {
        FileOutputStream fout = new FileOutputStream("B:\\address.txt");
        ObjectOutputStream obOut = new ObjectOutputStream(fout);   
                obOut.writeUTF(city);
                obOut.writeUTF(country);
        obOut.close();
        System.out.println("Output Done");
       } catch(Exception ex) {
           ex.printStackTrace();
       }
   }
}

Now, on “obOut.writeUTF(city); obOut.writeUTF(country);” I separated out two string inputs. Is there a way to combine them into one? As in obOut.writeUTF(city, counry) instead of two. Or is this only achievable through making these into an object?

[Update]
Imported a couple more and I tried

PrintStream ps = new PrintStream(new FileWriter("B:\\addressPS.txt")); 
ps.println(city); 
ps.println(country); 
ps.close(); 
But with errors, any clue?
  • 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-17T10:59:04+00:00Added an answer on June 17, 2026 at 10:59 am

    You are doing the right thing keeping them separate already. City and country are different fields.

    A very common mistake is not making a distinction between binary and text files/socket streams. You are a mixing the two which will lead to confusion. I suggest you only sue text Writer/Reader or binary Input/OuptutStream unless you have a very clear idea of what you are doing.

    In short if you what to write text use

    PrintStream ps = new PrintStream(new FileWriter(textFileName));
    ps.println(city);
    ps.println(country);
    ps.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have thousands of HTML files to process using Groovy/Java and I need to
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I've tracked down a weird MySQL problem to the two different ways I was
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I am using jsonparser to parse data and images obtained from json response. When
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.