Results 1 to 17 of 17
  1. #1
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Angry problems copying a file

    I've a problem copying an old file into a new one in an other path.

    Java Code:
                FileReader fr = new FileReader("C:\\Programmi\\NetBeans 6.7\\nb6.7\\testoprova.txt");
                File fileTemp = new File("C:\\temp_file.txt");
                if(fileTemp.createNewFile()) System.out.print("file creato con successo");
               
                FileWriter bwFile = new FileWriter(fileTemp);
                while((myBuffer=fr.read())!=-1){
                    //System.out.println(myBuffer);
                    bwFile.write(myBuffer);
                }
                bwFile.flush();
                bwFile.close();
    this is my old .txt file content :
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This is a test text.

    Questo un testo di prova."



    and this is my output in the created file:
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This is a test text. ������

    Questo � un testo di prova.������"


    so, why doesn't he recognize certain kind of char? The read() method should read the ASCII int value.. THEN WHY??
    I tried to use BufferedWriter and BufferedReader with the readline() method too... nothings different happened..:(

    i don't know other ways.. perhaps FileChannel class can help me?

    Remember that my program must copy file with any extension (.pdf , .jpg , .png [...]), NOT ONLY .txt!

    please help i don't know why this problem happens..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use a FileInputStream and FileOutputStream not a FileReader and FileWriter.

    All this character translation is completely unnecessary and counter-productive and is probably your problem.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Also, whether or not it can "read" the characters depends on the encoding in which the file was written and what the standard system file ecncoding is.

  4. #4
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    You can use org.apache.commons.io.IOUtils.copy() method.
    Try Controls4J - Advanced Swing Components.

  5. #5
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by devunion View Post
    You can use org.apache.commons.io.IOUtils.copy() method.
    But it's not in Java Standard libraries... is it? anyway that library is full of static methods which looks pretty useful in I/O operation..
    but for the moment i'm trying with FileInputStream..if i fail i'll try this other solution..thanks

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    And use the byte[] (and use an array of 8192, at least) version of the read and write methods. Reading and writing a single byte at a time is about as bad as you can get for performance.

  7. #7
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    And use the byte[] (and use an array of 8192, at least) version of the read and write methods. Reading and writing a single byte at a time is about as bad as you can get for performance.
    ok, i know.. indeed i tried the readline method of the BufferedReader (to read and write "line by line" :o)... but it didn't works...

    Anyway FileInputStream reads byte correctly from files and save them into byte[] correctly too... but the question is.. what should i do if i need to operate with files bigger than 8 KiloByte (8192) ? Java supports this case? or do i need to create my own class to use my personal byte [] b = new byte[8192]; as a "bucket"... filling and emptying it with bytes over and over until the end of file?

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    That's what Masi meant. Read 8k at a time so you copy faster without using too much memory.
    P.S The FileWriter is appropriate for writing characters. You should copy the files as binary data.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Also write using the write(byet[], int, int) method. You need to tell it which bits of the array to write, so in this case 0 to the amount read by the read(byte[]) method.

    If you don't do this you may find the odd file getting corrupted as it writes all the byte[] out, but the read() hasn't actually filled all 8192 bytes.

  10. #10
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Also write using the write(byet[], int, int) method. You need to tell it which bits of the array to write, so in this case 0 to the amount read by the read(byte[]) method.
    uhm.. sorry.. what do you mean by "You need to tell it which bits of the array to write" ? do you meant BYTE? are you talking about the second parameter of the write(byte[], int, int) method ?..

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Sorry...:)
    "bits" as in "parts"...as opposed bits.
    Bad choice of word on my part there.

    I'll expand a bit...um...slightly.

    When you do the read(byte[]) it will read data into the byte array, but it may not entirely fill the array, so you need to note the return value, which says how much it's read. This return value is used for the length parameter of the write() call (the last parameter). You won't want an offset, so that one will always be 0.

    If you don't do that you may end up with a corrupted file.

  12. #12
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    AHH! ok now everythings is clear.. :) thanks for the advise about the read() return value..

    Quote Originally Posted by Tolls View Post
    I'll expand a bit...um...slightly.
    Very funny! D

    i have not solve this problem yet but i'm on my way to solve it..

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    I only mention it because I got caught out by it a couple of weeks ago.

    Bit of code to move a zip file from where it was generated to where it could be accessed by the user. One of the files was corrupted on the way and we noticed it was an exact multiple of the byte[] size we were using...went back to the code, changed it to take into account the amount read in and...voila!

    I like quick 'n' easy fixes.

  14. #14
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    well.. i was thinking about a solution which allow me to use a byte[] buffer bigger than 8192.. so i write a static method like this:
    Java Code:
        public static int getMaxSizeExpByteArray(){
            byte [] b;
            int i = 15;
            boolean exit = false;
            while(!exit){
                try{
                    b = new byte[(int)Math.pow(2,i)];
                    i++;
                }
                catch( java.lang.OutOfMemoryError ex) {i=i-2; exit=true;}
            }
            return i;
        }
    Ok.. i know that a method like this one is the DarkSide of java programming :).. but with this i can read more data .. iteration by iteration .. because my byte[] array is very bigger than 8192.. and at the same time i'm sure to never receive an heap out of memory error....

    any comments?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Believe me, that'll take longer to figure out what the maximum is than using 8192.
    1024 can transfer a meg or so in under a second on my PC.

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by atom86 View Post
    well.. i was thinking about a solution which allow me to use a byte[] buffer bigger than 8192.. so i write a static method like this:
    Java Code:
        public static int getMaxSizeExpByteArray(){
            byte [] b;
            int i = 15;
            boolean exit = false;
            while(!exit){
                try{
                    b = new byte[(int)Math.pow(2,i)];
                    i++;
                }
                catch( java.lang.OutOfMemoryError ex) {i=i-2; exit=true;}
            }
            return i;
        }
    Ok.. i know that a method like this one is the DarkSide of java programming :).. but with this i can read more data .. iteration by iteration .. because my byte[] array is very bigger than 8192.. and at the same time i'm sure to never receive an heap out of memory error....

    any comments?
    Until you do a few other things that also use heap space.

    It is not about getting the latgest buffer possible, not at all, it is about finding a buffer size that is optimal with the disk read and write routines. 8192 is usually a decent value for that.

  17. #17
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Alright, i fulfill this task and i manage to operate with bigger (up to 64 MB) file writing and reading them in a LONGBLOB field in MySQL database... and yes i have to admit that for the moment a 2^13 length buffer works fine.. thanks to everyone for the advises.. everytime useful!

Similar Threads

  1. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  2. Copying a derectory
    By linux1man in forum New To Java
    Replies: 10
    Last Post: 01-15-2009, 07:16 PM
  3. Creating Folder in JAVA and Copying File
    By fnoman in forum New To Java
    Replies: 8
    Last Post: 10-18-2008, 07:33 AM
  4. Replies: 13
    Last Post: 01-09-2008, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •