Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: File input and output

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File input and output

    I could really do with some help on an assignment. This is the code i have so far. Next, I am trying to write what is in the input file, into the output file. I would be very grateful if anyone could help.

    package booksca;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class BooksTest 
    {
     
        public static void main(String[] args) 
                throws FileNotFoundException, IOException
        {  
            //Creates an ArrayList
            ArrayList list = new ArrayList();
     
     
            Scanner console = new Scanner(System.in);
     
            //Prompts the user to enter the input file name
            System.out.print("Input file: ");
            String inputFileName = console.next();
     
            //Prompts the user to enter the output file name
            System.out.print("Output file: ");
            String outputFileName = console.next();
     
            //Creates a new File object
            File inFile = new File(inputFileName);
     
            try
            {
     
            //Constructs a new Scanner object 
            Scanner in = new Scanner(inFile);
     
            while (in.hasNextLine())
            {
                FileReader fileReader = new FileReader(inputFileName);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
     
                String title = in.nextLine();
                String authorFName = in.nextLine();
                String authorLName = in.nextLine();
                double price = Double.parseDouble(in.nextLine()); 
                int year = Integer.parseInt(in.nextLine());
     
                //Creates a new Book object
                Book b1 = new Book(title, authorFName, authorLName, price, year);
                System.out.println(b1.getTitle());
                System.out.println(b1.getAuthorFName());
                System.out.println(b1.getAuthorLName());
                System.out.println(b1.getPrice());
                System.out.println(b1.getYear());
     
                list.add(bufferedReader.readLine());      
            }   
            }
     
            catch (FileNotFoundException ex)
            {
                System.out.println("File not found: " + ex.getMessage());
            }
     
     
            //Constructs PrintWriter object with the name if the output file, in order to write to the file   
            PrintWriter out = new PrintWriter(outputFileName);
            /* int lineNumber=1;
     
            while (in.hasNextLine())
            {
                String line = in.nextLine();
                out.println(" ");
                in.close();
                out.close();
            } */           
        }
    }

    package booksca;
     
    public class Book {
     
        private String title;
        private String authorFName;
        private String authorLName;
        private double price; 
        private int year;
     
        public Book(String t, String fn, String ln, double p, int y){
            title = t;
            authorFName = fn;
            authorLName = ln;
            price = p;
            year = y;
     
        }
     
     
        public String getTitle() {
            return title;
        }
     
        public void setTitle(String title) {
            this.title = title;
        }
     
        public String getAuthorFName() {
            return authorFName;
        }
     
        public void setAuthorFName(String authorFName) {
            this.authorFName = authorFName;
        }
     
        public String getAuthorLName() {
            return authorLName;
        }
     
        public void setAuthorLName(String authorLName) {
            this.authorLName = authorLName;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setPrice(double price) {
            this.price = price;
        }
     
        public int getYear() {
            return year;
        }
     
        public void setYear(int year) {
            this.year = year;
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File input and output

    I am trying to write what is in the input file, into the output file.
    What happens when you compile and execute the code?
    What does the program print out?
    What was written to the output file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File input and output

    The program prints out the contents of the input file, if there is no such file it gives you a message.
    Nothing writes to the output file. If there is anything in the output file, it deletes the content.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File input and output

    Nothing writes to the output file
    What method(s) of the Printwriter class have you tried to use? What happened when you used them?
    The code has been commented out.

    Here's a suggestion:

    Write a small program that uses the PrintWriter class to write something to a file. Compile it and test it. When it works like you want, then use the code and techniques learned to write the code in this program.
    To append to an existing file, you need to use a class that has an option for appending data to an existing file. The FileOutputStream class has that option. Wrap a call to that class in the PrintWriter's constructor, since the PrintWriter has a constructor that takes an OutputStream.
    PrintWriter out = new PrintWriter(new FileOutputStream(...
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. File input & output
    By a21j92 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 8th, 2013, 04:13 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  4. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM

Tags for this Thread