• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copy file

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any special reason that the count of member in byte array is 1024?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try changing the size of the byte array?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

What they do?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One ring to rule the mall?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hahaha

What the hell, I actually sat here sniggering for a good minute.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which code is better? The first that I posted or this?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woops, I missed that he was using a Reader.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileReader and FileWriter use byte streams underneath the hood.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because obviously the default encoding doesn't support Farsi.

You need to use InputStreamReader instead.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
definitely I can save a file so I choose the encoding,

Look at this image please

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check the "file.encoding" system property of your JVM if you want to know for sure.



How to check it?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The System class has methods for accessing system properties.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to append new lines, Then It must be true
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the final result you want? A simple copy of your original file?

Then you should set append to false.
 
reply
    Bookmark Topic Watch Topic
  • New Topic