简体   繁体   中英

java I/O try with resources for binary data parameters?

I'm rather confused on the parameters for reading and writing binary data. When reading and writing bytes containing ASCII caharacters I understand the format is something like this try(FileInputStream fin = new FileInputStream(args[0])) . Where its mostly console arguments.

But I see that for the try with resources in reading and writing binary data its

try(DataInputStream dataIn = new DataInputStream(new FileInputStream("testdata"))) &

try(DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("testdata"))

Why is new FileInputStream("testdata") written like this ? Why is it created as an object inside and what is "testdata" suppose to mean ?

I/O libraries can be very confusing because different communications have different needs. The architecture is intended to keep as much in common as possible. The various subclasses and wrapper classes either add characteristics (such as buffering) or processing (such as text encoding/decoding or datatype transformations).

(Sometimes it seems like the architecture is too fractured because each class adds so little and it takes two or three classes to do very common things. Commonly used 3rd-party libraries can make it easier.)

Think of it this way, if the object you have is awkward in some way, find the class in the library that makes it easier to use in the way you need. You haven't shown any code that does anything so I can't point to a specific advantage to using the classes that you mention.

"testdata" would be the name of what your operating system presents to your user's Java as a "file". In the common case, it is the name of a file, in what would be the current working directory for your program. Which directory that is might depend on how your users start your program. (Perhaps you are expecting a file name to have an extension."

Tip: Use an IDE that presents JavaDoc

Tip: Use an IDE that allows refactoring, particularly, extract local variable. That gives you a chance to pull out expressions that are confusing to you and give them a name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM