Simple example to use string in different ways.
Syntax :
String variable_name = "Hello World!"
Example :
- import java.io.*;
- public class CopyFile {
- public static void main(String args[]) throws IOException
- {
- FileInputStream in = null;
- FileOutputStream out = null;
- try {
- in = new FileInputStream("input.txt");
- out = new FileOutputStream("output.txt");
- int c;
- while ((c = in.read()) != -1) {
- out.write(c);
- }
- }finally {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- }
- }
- }
Description :
Step 1:Starting of class
Step 2:Start of main function.
Step 3:The input or output type of exceptions that occur in main function are thrown away.
Step 4 :There are many classes for byte stream but FileInputStream and FileOutputStream are most frequently used. Here input.txt is the name of actual file that is present in your machine.
Step 5:Here output.txt is the file that will be created .
Step 12: Read every character from file "input,txt" .
Step 13:Write every character in file "output.txt" .
Step 15 - 18: "finally" keyword is used when all try catch operations are completed. If input,txt not closed then in.close closes input.txt file. Same for output.txt file,
Step 12: Read every character from file "input,txt" .
Step 13:Write every character in file "output.txt" .
Step 15 - 18: "finally" keyword is used when all try catch operations are completed. If input,txt not closed then in.close closes input.txt file. Same for output.txt file,