4 ms·
Working with File Handling in Java
- lutusp 14y agoA quote from the article: ----------------------------------------------- File is a collection of bytes stored in secondary storage device i.e. disk. Thus, File handling is used to read, write, append or update a file without directly opening it. Types of File: Text File Binary File Text File contains only textual data. Text files may be saved in either a plain text (.TXT) format and rich text (.RTF) format like files in our Notepad while Binary Files contains both textual data and custom binary data like font size, text color and text style etc. ----------------------------------------------- This is wrong in so many ways that it takes one's breath away. First, one cannot read or append to a file without implicitly opening it. Second, the distinction between text files and binary files is only an issue on the Windows OS, which treats line endings differently in text files. Everywhere else, the existence of a flag to distinguish a text file from a "binary" file serves no purpose and doesn't do anything. Third, the above remarks about binary files are simply wrong. > // true if the file is executable > Boolean bool = f.canExecute(); 1. Do try to name your variables in meaningful ways. A Boolean named "bool" conveys no meaning whatever. 2. "canExecute()" doesn't really do what you seem to think. It may return true for an non-executable file, or false for an executable one. It only reports what the OS has been told to believe about the file. In your "Listing 2" (in fact, in all your listings), you rely on error trapping to control the flow of execution. This is a bad idea -- use explicit code to control program flow, for example, in this case you could have used f.exists() to determine that the file exists before trying to delete it. In summary, before writing tutorials, it's considered good form to learn the topic first.