w3resource

Java I/O Programming: Exercises, Practice, Solution

Java Input-Output Exercises [18 exercises with solution]

1. Write a Java program to get a list of all file/directory names from the given. Go to the editor
Click me to see the solution

2. Write a Java program to get specific files by extensions from a specified folder. Go to the editor
Click me to see the solution

3. Write a Java program to check if a file or directory specified by pathname exists or not. Go to the editor
Click me to see the solution

4. Write a Java program to check if a file or directory has read and write permission. Go to the editor
Click me to see the solution

5. Write a Java program to check if given pathname is a directory or a file. Go to the editor
Click me to see the solution

6. Write a Java program to compare two files lexicographically. Go to the editor
According to Wikipedia:
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order over the sequences (often called words in computer science) of elements of a finite totally ordered set, often called alphabet.
Click me to see the solution

7. Write a Java program to get last modified time of a file. Go to the editor
Click me to see the solution

8. Write Java program to read input from java console. Go to the editor
Click me to see the solution

9. Write a Java program to get file size in bytes, kb, mb. Go to the editor
Click me to see the solution

10. Write a Java program to read contents from a file into byte array. Go to the editor
Click me to see the solution

11. Write a Java program to read a file content line by line. Go to the editor
Click me to see the solution

12. Write a Java program to read a plain text file. Go to the editor
Click me to see the solution

13. Write a java program to read a file line by line and store it into a variable. Go to the editor
Click me to see the solution

14. Write a Java program to store text file content line by line into an array. Go to the editor
Click me to see the solution

15. Write a Java program to write and read a plain text file. Go to the editor
Click me to see the solution

16. Write a Java program to append text to an existing file. Go to the editor
Click me to see the solution

17. Write a Java program to read first 3 lines from a file. Go to the editor
Click me to see the solution

18. Write a Java program to find the longest word in a text file. Go to the editor
Click me to see the solution

 

Java Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Java: Tips of the Day

Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

if (a != 0 && b != 0)

will compile to 2 memory loads and two conditional branches

if (a * b != 0)

will compile to 2 memory loads, a multiply and one conditional branch.

The multiply is likely to be faster than the second conditional branch if the hardware-level branch prediction is ineffective. As you increase the ratio ... the branch prediction is becoming less effective.

The reason that conditional branches are slower is that they cause the instruction execution pipeline to stall. Branch prediction is about avoiding the stall by predicting which way the branch is going to go and speculatively choosing the next instruction based on that. If the prediction fails, there is a delay while the instruction for the other direction is loaded.

Ref: https://bit.ly/3ftdl2g