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);
        }
    }
}

Recommended Answers

All 6 Replies

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

print throws an exception because there is no print method. There is however a println method.

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”.

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)

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);
        }
    }
}

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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.