aspose file tools*
The moose likes Beginning Java and the fly likes Copy file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copy file" Watch "Copy file" New topic
Author

Copy file

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635

Is there any special reason that the count of member in byte array is 1024?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3091
    
  33

Did you try changing the size of the byte array?


Mohamed Sanaulla | My Blog
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 73
I think it's just to limit the readings to 1 kb, it's not a good idea to have an enormous array in the memory but also it's not a good idea to have a lot of disk readings so that value should bring a balance to that.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

abalfazl hossein wrote:Is there any special reason that the count of member in byte array is 1024?

Not really. 1024 bytes = 1 KB. I think it's a rather small number but it's an arbitrary choice.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


May you explain about args[0] and args[1]?

What they do?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Look at the main method in line 4. Do you see the args there? It is a String array that contains the arguments that you enter on the command line. Ofcourse, args[0] is the first, and args[1] is the second command line argument. If you run the program like this:

java JCopy sourcefile destfile

then args[0] will be sourcefile and args[1] will be destfile.
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 73
I also add to mister de Jong answer that the argument of the main function may be called whatever you want, as long as it is a String[] and satisfies the variable naming rules, so for example if you are spanish as me, you can put this

public static void main(String[] parametros)

or

public static void main(String[] oneringtorulethemall)

bet everywhere you will see args as this is the default name that IDEs like eclipse puts.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

One ring to rule the mall?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hahaha

What the hell, I actually sat here sniggering for a good minute.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Which code is better? The first that I posted or this?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

The first one buffers the input before writing it. Your last code doesn't.

They should both work fine, except the first program should be faster. However, the code is also more unclear.

For copying files, I recommend taking a look at the transferTo/transferFrom functions in FileChannel.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
Streams work with all files (binary and text/character), whereas Readers and Writers are only guaranteed to work with text files - so the codes are not equivalent.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Woops, I missed that he was using a Reader.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


new file, only create an object.No stream!
It is used to use byte stream and after that character stream to write on it. But in this code there is no byte stream:




May someone explain about that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

FileReader and FileWriter use byte streams underneath the hood.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Then, FileWriter creates character stream,But I think that in compare to inputstreamreader, When you use InputstreamReader you can choose the encoding that you want.

FileReader always uses the platform default encoding which is generally a bad idea.


http://stackoverflow.com/questions/696626/java-filereader-encoding-issue

Why it is bad idea?
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 73
well... the bad thing about that is that is less portable.

for example you could not read a chinese file with filereader. (you'd read it but not correctly)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


When I wrote farsi in mytext and run this program, What I see in mytextcopy is:

?? ?? ?? ??? ?? ???

If it uses defult encoding of Operation System, Then why the result is like this?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
or example you could not read a chinese file with filereader. (you'd read it but not correctly)


Thanks Alex!

I guess we posted at same time, I didn't see your post,But still I have question:

If FileWriter uses defult encoding of Operation System, Then why the result is like this?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Because obviously the default encoding doesn't support Farsi.

You need to use InputStreamReader instead.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I ran this program on windows XP. In XP, windows 1252 encoding is used.Then, If that program doesn't work becuase it used defualt encoding, It must not work with windows 1252 encoding.But When I ran, I see it works:


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

It appears that the default encoding isn't Windows-1252. I don't know what is, but I don't really see what the problem is :P
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


I did this manually: At first I open the file, Then I change the encoding to unicode. and so for mytextcopy. and after that I ran this program:




What I see in files is:

Êæ


and When I check the encoding of files, I see that encoding came back to default: ANSI

Another way, It is said that when you choose FileWriter, It chooses deault encoding that is used by Operation System.The default encoding that is used for txt files in windows is ANSI.I think that that is why the encoding of file return back to ANSI, After the running of the program.

Is my guess correct?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

The encoding is not stored inside the text file itself. Programs have no way to know for sure what the encoding of a text file is. Many text editors try to detect the encoding by looking at what bytes the file contains, but they can't always guess it correctly. Your text editor didn't automatically recognise the encoding and so it assumed that it's probably ANSI, which was a wrong guess in your case.

So, in your Java program you can use any encoding you want, but what encoding was used isn't explicitly stored in the file itself - programs opening the file don't know for sure what encoding is used for that file.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
definitely I can save a file so I choose the encoding,

Look at this image please

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
abalfazl hossein wrote:definitely I can save a file so I choose the encoding

That image actually illustrates Jesper's point: you have to know (and specify) which encoding to use. It is not then stored explicitly with the file, only implicitly by using it for converting characters to bytes. You should read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) to get a better idea of what file encodings are for, and how they are used.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks, But you didn't answer my question

and When I check the encoding of files, I see that encoding came back to default: ANSI

Another way, It is said that when you choose FileWriter, It chooses deault encoding that is used by Operation System.The default encoding that is used for txt files in windows is ANSI.I think that that is why the encoding of file return back to ANSI, After the running of the program.

Is my guess correct?


and another question, Why does it return to ANSI?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
abalfazl hossein wrote:When I check the encoding of files, I see that encoding came back to default: ANSI

Where do you see that? What piece of software tries to determine the encoding?

The default encoding that is used for txt files in windows is ANSI.

I doubt that ANSI is Java's default encoding on Windows; something like CP-1252 seems more likely. Of course, if a file contains ASCII only, then you won't see a difference. So if a file contains ASCII only, then ANSI is a safe choice. Conversely, if a text file contains a byte order mark, then it's likely to be in Unicode, and the exact encoding can be guessed at.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Where do you see that? What piece of software tries to determine the encoding?


Please look at the image again Sir, In the bottom of image, I can choose Unicode, UTF8, ANSI...

FileReader always uses the platform default encoding which is generally a bad idea.

http://stackoverflow.com/questions/696626/java-filereader-encoding-issue

The default for txt file is ANSI in windows, While using FileReader, JAVA uses default encoding of operation system. That is here ANSI.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
abalfazl hossein wrote:Please look at the image again Sir, In the bottom of image, I can choose Unicode, UTF8, ANSI...

Yes, YOU can choose an encoding. That doesn't say anything about the contents of the file. If you happen to choose an inappropriate encoding, the application (or the JVM) will blindly do as you told it to, likely with bad results.

The default for txt file is ANSI in windows

As I said, I doubt that. Check the "file.encoding" system property of your JVM if you want to know for sure.

While using FileReader, JAVA uses default encoding of operation system. That is here ANSI.

What is "that ... here"? Are you talking about a specific file?

Actually, at this point I'm not sure what the problem is. You seem to understand that Java uses a default encoding if your code doesn't specify a different one. I mentioned above how you can find out which encoding that is. If the default isn't appropriate for whatever data you have, then you do need to specify the encoding explicitly; have you tried that?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Check the "file.encoding" system property of your JVM if you want to know for sure.


How to check it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
The System class has methods for accessing system properties.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
You're correct. It is Cp1256


Then There is question for me now: If the default is Cp1256
, Then why in the notepad there is no Cp1256?

You can select only: ANSI-UTF8, Unicode,Unicode big indain
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
CP1256 is Arabic; it's unlikely that a simple general-purpose application like Notepad would know how to handle that. UTF-8 is generally a good choice, especially as all JVMs are required to support it.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in=null;
BufferedWriter out=null;
// Open file to be copied
in=new BufferedReader(new InputStreamReader(new FileInputStream("mytext.txt"),"UTF8"));
// And where to copy it to
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("mytextcopy.txt",true),"UTF8"));

String line = null;
while ((line=in.readLine()) != null) {
out.write(line);
out.newLine(); // Write system dependent end of line.
}


// close both streams
in.close();
out.close();

}
}


When I opened the mytextcopy, I see it wrote in the same line.But because of newline(),The content of mytext must be in new line.
why isn't in new line?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Don't understand your last error. What was the input, what was the output, and what did you expect as output?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
This program reads from mytext, and write to mytextcopy.

I guess that the lines that copies from mytext, Begins from the new line, But it doesn't!

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Try setting the boolean in the FileOutputStream constructor to false.

You may be mistaking your output for erroneous output that already existed in the file.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I want to append new lines, Then It must be true
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

What's the final result you want? A simple copy of your original file?

Then you should set append to false.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy file