i'm trying to copy all txt files from a directory into one file to another directory.
But i have the following error:
"FileStreamsTest: java.io.FileNotFoundException: . (Accès refused)""

My txt files are located in the project folder of NetBeans.

here is my code:

package FileStreamTest;
import java.io.*;

class FileStreamsTest {
    public static void main(String[] args)
            File here = new File(".");
            for(File content : here.listFiles()) {
            if(content.isFile() && content.getName().endsWith(".txt"))
            //File inputFile = new File("Text1.txt");
            //File inputFile2 = new File("Text2.txt");
           // File inputFile3 = new File("Text3.txt");
            File outputFile = new File("TextOutput.txt");

            FileInputStream fis = new FileInputStream(here);
           // FileInputStream fis2 = new FileInputStream(inputFile2);
           ///FileInputStream fis3 = new FileInputStream(inputFile3);

            FileOutputStream fos = new FileOutputStream(outputFile);
            int c;
           // int b;
            //int a;

            while ((c = fis.read()) != -1)

           // while ((b = fis2.read()) !=-1)

           // { fos.write(b);
           // }

           // while ((a = fis3.read()) !=-1)
               // fos.write(a);

           // fis2.close();
            catch (FileNotFoundException e)

            System.err.println("FileStreamsTest: " + e);
            catch (IOException e)
            System.err.println("FileStreamsTest: " + e);

Thank you

6 Years
Discussion Span
Last Post by bibiki

Would be useful to know which file caused the refused access - especially if it was the output file. A few debugging print statements would help.


ok, the only error is :

FileStreamsTest: java.io.FileNotFoundException: . (Accès refused)
BUILD SUCCESSFUL (total time: 0 seconds)"

There is no indication about the file for which the access is refused.

Thank you


A few debugging print statements would help.

eg print something (including the file name) immediately before & after trying to open/read/write each file. That way you can see which file it was trying to open/read/write when the error occurs


I tried to print the file name before and after trying to open/read/write each file but i have the error.


That seems unlikely - you shouldn't get a FileNotFoundException before you try to do any kind of file access. In your catch blocks rather than printing the short exception message, get the full stack trace
This will tell you exact line of your program where the error was first detected (in general you should always have a full printStackTrace() for any exception when you first write a program - the info it gives is essential for debugging)


OK with e.printStackTrace();
i obtain this error :

FileStreamsTest: java.io.FileNotFoundException: . (Access refused)
java.io.FileNotFoundException: . (Access refused)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at FileStreamTest.FileStreamsTest.main(FileStreamTest.java:23)
BUILD SUCCESSFUL (total time: 1 second)

Thank you


line 29 of your code contains an assignment operator (=), while I believe you intended to have a comparison one (==) check whether that helps. plus, lines 12 and 13 contain brackets, they are either unnecessary or the bracket on line 13 should have been somewhere else.


Hi bibiki
Line 29 is the normal way to read a char and test for EOF - it's OK as written.
I think the {} on 12/13 are just an artefact of the various things the OP has tried.
I'm currently trying to help him to learn how to debug this kind of thing - so he can get to the obvious error (I'll PM you what it is if you like) and gain some useful skills at the same time. If you've spotted the error, please help OP to find it for himself, don't just tell him what it is


i removed the bracket on line 13 and put it immediately after line 50, and the error was removed. however, the content on TextOutput remains void.


James, I know what you mean.
I missed his declaration of int c variable, so I jumped into conclusion there. I am myself learning, and sticking my nose here to learn more than to teach. Anyways, I don't think I gave an answer.. it was just a suggestion. I am waiting to see what turns to be the solution.


OK guys - its 23:00 here and I'm going to bed now. Here's a final clue:

Line 29 if trying to read stream fis which is created on line 20. It's supposed to be a file in the directory, but look closely at line 20 - what File is that???

Good night

ps: bibiki - your contribution IS valuable and welcome. Please keep contributing. Maybe you can finish this one off while I get some sleep!

Edited by JamesCherrill: ps


Thank You Bibiki,
in fact i tried to remove the bracket on line13 but, i always have the same error like:

FileStreamsTest: java.io.FileNotFoundException: . (Access refused)
java.io.FileNotFoundException: . (Access refused)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at FileStreamTest.FileStreamsTest.main(FileStreamTest.java:21)

But in fact, in the beginning i just wanted to read 3 text files and put the content in one files, it was working well.
And now i just trying to get all text files and put contents in one file, that's why there are many line in comments.

But however The e.printStackTrace is very useful.


what do you mean e.printStackTrace is useful? did that solve your problem? if yes, how?


no no as i'm beginner in java, i just say e.printStackTrace was useful.
i 'm always trying to understand my code


Good morning. Things have obviously stalled here.
Line 20 you open the input stream using the file "here" - but that's the File you used to hold the directory, and you can't read a directory with an input stream!

line 10 is good:
for(File content : here.listFiles()) {

so line 20 needs to be
FileInputStream fis = new FileInputStream(content);

Get that working and we'll then move on to the next pproblem (it's to do with the output file...)

Debugging Exceptions summary: printStackTrace tells you what error and where (line number). Look at that line. If it's not obvious use System.out.println(...) to print the variables and expressions used on that line so you can see what exactly has gone wrong.


Thank you James,

for your quick reply this morning.
I will check out this evening when i'll be at home.

Best Regards


I put a system.out statement inside the while loop in lines 30-32, and found out that fis.read() returns numerical values. I expected it to return text. however, the numbers are not input in the textOutput file either. I do not know what's going on... could it be .write() method is not what we need or what? James?


Hi Bibiki,

your method works and i get text in the output file : textOutput.txt but only if i stop the execution of the program at half time.

If i let terminate the program by itself, the file textOutput.txt is void.


the read method reads one byte from the stream and returns it in the bottom 8 bits of an int variable, so that's why you get numbers. If the file happens to contain ASCII text then these numbers will correspond to the ASCII codes for each character.
write works similarly - takes the low-order byte and writes it to the output stream.
I've never tried doing it that way, because either I would use a BufferedReader to read whole lines of text and write them out to a PrintWriter, or I would read & write whole arrays of bytes if the file was more complex than just lines of text. However, I think what you're doing should work, maybe just a bit slower.
Try reducing the file copy part to its simplest form (copy one very short fixed input file to an output file) and get that working before you try to wrap it in the directory-traversing multiple file case.


You re-open the output stream each time you go thru the multiple file loop - this erases the previous contents each time. You should open the output stream before entering the multiple file loop, and only close it after the last file has been copied. (You could alternatively open it in append mode on each pass of the loop - but thats inefficient.)

Edited by JamesCherrill: n/a


I just recently had to do a project at school manipulating files. And, I'd use for this project the classes and methods I used for that other project, and I know they would work perfectly fine. the classes are:
FileWriter y = new FileWriter("TextOutput.txt");
FileReader x = new FileReader("aFileInputIsTakenFrom");
BufferedReader input = new BufferedReader(x);
PrintWriter outfile = new PrintWriter(y);
out of curiosity, I wanted to understand these other classes Neversleepin has used, but I can't make any sense out of all this. I pulled these two lines:
File outputFile = new File("TextOutput.txt");

FileOutputStream fos = new FileOutputStream(outputFile);
out of the for loop (I also pulled out of the for loop the fos.close() method). however, my while loop now diverges, and the content inside TextOutput.txt remains void...


OK Guys thank you very much for your help.
In the same time i was trying another way to get the same output and it's working very well.

In fact, i use the logger package.
1-i try to Create the output file.
2-i try to write the content of the list of file
3-after i use the logger tutorial and i try to write the output and it's working as i want.
but the code seems very complex for me.

Thanks a lot Bibiki and James.


Yes, BufferedReader / PrintWriter is the "standard" way to copy text files, but I wouldn't rely on them for files with arbitrary binary data. For files in general I'd use DataInputStream and DataOutputStream, reading/writing in decent sized blocks (byte arrays) of maybe 4k bytes.
Even so, it looks to me like NeverSleepin's version should work, once the surrounding logic is right.

ps: I meant the original version - using Logger seems a very round-about way to do things to me; this program can be written with pretty simple & obvious code using just the classes/methods mentioned above

Edited by JamesCherrill: ps - just saw post re Logger



when i execute the code with NetBeans i can stop it before it finishes.
try to click Stop and open the textOutput.txt,you will see some text but it's partly filled.


NeverSleepin, I don't use NetBeans. Anyways, as far as a functioning app goes, I think I can have one. I was just sticking my nose here to see your alternative. thanks for your replies, as well. I think you could and should mark your thread as solved since you solved your problem.

James, your level of expertise is obviously way to higher than mine. While you do understand yourself with a great comfort, I am having trouble to :P
Anyways, I shall find me a solution for this particular way of doing the task.

regards to you all.


Yo bibiki - here's an example of the way to copy any file with reasonable efficiency.

static void copyFile() {
      JFileChooser fc = new JFileChooser();
      File inFile = fc.getSelectedFile();
      File outFile = fc.getSelectedFile();
      System.out.println("Copying " + inFile.getName() + " to " + outFile.getName());
      try {
         DataInputStream inStream = new DataInputStream(new FileInputStream(inFile));
         DataOutputStream outStream = new DataOutputStream(new FileOutputStream(outFile));
         byte[] data = new byte[4096]; // arbitrary reasonable size buffer
         int count;
         while ((count = inStream.read(data)) != -1) {
            outStream.write(data, 0, count);
      } catch (IOException ex) {

thank you James. I just tried your code. I think the while loop is diverging or something. but no worries, I'll figure my way around both, your code as well as that of neversleeping. take care!

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.