0

Hello, so I have this program which should be creating a file called output and appending some content from another file,data.txt, accordingly. Creating file called output works but then the output file is empty. what am i doing wrong?

Oh and Im suppose to use method print() but that gives me a bunch of errors that says its not applicable.

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException; 
import java.io.FileWriter;

class TestFile2{
    public static void main(String[] args) {
        try { 
            PrintWriter out = new PrintWriter(new FileWriter("output", true));
            File f1 = new File("c://Java/Lab 1/MyDocument/data.txt");
            File f2 = new File("MyDocument", "data.txt");
            File d1 = new File("MyDocument");

            if(f1.isDirectory()){
                out.print(f1.getName() + " is a directory");
            }else if(f1.isFile()){
                out.print(f1.getName() + " is a file");
            }

            if(d1.isDirectory())
                out.print(d1.getName() + " a directory");

            if (f1.exists()){
                FileReader in = new FileReader(f1);//object FileReader that refers to f1
                int data = in.read(); //read a char at a time
                while (data != -1) {
                    out.write(data);
                    data = in.read();
                }
                out.print();
                System.out.flush();
                in.close();
            }

        } catch (Exception e) {
            System.out.println("Exception: "+e);
        }
    }
}

Edited by nadiam: edit code and extra info

3
Contributors
6
Replies
38
Views
2 Months
Discussion Span
Last Post by JamesCherrill
0

Okay, update. i didnt have out.close() so it couldnt add what i wanted to the file. but how do i use method print() ?

0

Oh.. hmm how come we've been asked to use print() method? unless we're suppose to figure the whole no such thing as print() method?

iv. Use method print() to write the characters read (the file content) to file “output”.

0

print throws an exception because there is no print method

???

PrintWriter has 9 overloaded print methods- one for each of the primitives plus ones for Object and String - just look at the API doc
https://docs.oracle.com/javase/8/docs/api/java/io/PrintWriter.html

however, they all require a parameter of some sort, so what tinstaafl should have said is

print() throws an exception because there is no print() method

and so the requirement statement is reasonable, but badly phrased, in that you should use a print method (eg print(char c)), but not print()

(see the difference between line 18 and line 31)

Edited by JamesCherrill

0

Ohh.. OKay. Thank you so much. So i did this:

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException; 
import java.io.FileWriter;

class TestFile2{
    public static void main(String[] args) {
        try { 
            PrintWriter out = new PrintWriter(new FileWriter("output"));
            File f1 = new File("c://Java/Lab 1/MyDocument/Data.txt");
            File d1 = new File("MyDocument");

            if(f1.isDirectory()){
                String s = f1.getName() + " is a directory";
                out.print(s);
            }else if(f1.isFile()){
                String s = f1.getName() + " is a file";
                out.print(s);
            }

            if(d1.isDirectory()){
                String s = d1.getName() + " a directory";
                out.print(s);
            }

            if (f1.exists()){
                FileReader in = new FileReader(f1);//object FileReader that refers to f1
                int data = in.read(); //read a char at a time
                while (data != -1) {
                    out.write(data);
                    data = in.read();
                }
                out.print(data);
                System.out.flush();
                out.close();
            }

        } catch (Exception e) {
            System.out.println("Exception: "+e);
        }
    }
}
0

You must be more careful about confusing write and print methods like that (lines 32, 34).

public void write(int c)
Writes a single character (whose UniCode value is in the int).

public void print(int i)
Prints an integer as a string showing the value of the int.

so if i is 74...
write(i) will write the character 'J'
print(i) will write the string "74"

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.