Google+

272. System.out.println( )








System is a predefined java class in which a static object out of PrintStream I/O Class is created as shown below -

public class System
{
    static PrintStream out = new PrintStream(//constructor connected to standard output stream);
}

public class PrintStream
{
    //Code for print( ) and println( ) methods etc. 
}

As out is the static object inside System Class, hence we can access the out object from other Classes by using its Class name

 i.e. System.out

And as print( ) and println( ) method are the members of the PrintStream Class, we can access it using its object called out as shown below -

System.out.print("Text to be printed here");
System.out.println("Text to be printed here");

So, we have now understood how a System.out.println(" "); statement works. Please find the example program which uses System.out.println(" "); statement as shown below -

Example -

public class ClassName
{
      public static void main(String args[])
      {
              System.out.println("Hi. How are you?");
      }

}

Output -

Hi. How are you?






Please comment below to feedback or ask questions.

static import will be explained in the next post.










271. Using PrintStream I/O Class









print( ) and println( ) methods which we use for printing the output are the methods of the PrintStream Class. By default when these methods are used with System.out will print the output in the console. But now lets try to implement PrintStream Class for printing the output into a text file using print and println statements as shown in the below example -

Example -

PrintStream ps = new PrintStream(new FileOutputStream("abc.txt"));

ps.println("Hi. How are you?");

ps.print("I am doing good");

Output (printed into 'abc.txt' file) -

Hi. How are you?
I am doing good.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'PrintStreamDemo.java' with main( ) method in the existing Java Project 'Project 51' as shown below -


2. Create a PrintStream Class object statement by specifying the 'abc.txt' as an output file as shown below (Resolve import errors) -


3. Add 'throws IOException' to the method declaration to resolve the error as shown below (Resolve import error)  -


4. Use println( ) and print( ) methods with the PrintStream Class object as shown below -


5. Close the PrintStream object as shown below -


6. Save & Run the program and observe that println( ) and print( ) methods printed the specified texts as output into the 'abc.txt' file in our project workspace as shown below -



Hence we can use println( ) and print() methods of PrintStream Class to print the output of a program to a desired destination like files etc. In this post, I have explained to print the output to a file. We can also print the output to the other destinations like console etc.







Please comment below to feedback or ask questions.

System.out.println() will be explained in the next post.








270. Reducing the object creation statements









We can reduce the object creation statements as shown in the below examples -

Example1 -

Original -

File file1 = new File("abc.txt");

FileWriter fw = new FileWriter(file1);

We can reduce the above two sentences to the below single sentence -

FileWriter fw = new FileWriter(new File("abc.txt"));


Example2 -

Original -

File file1 = new File("abc.txt");

FileWriter fw = new FileWriter(file1);

We can reduce the above two sentences to the below single sentence -

FileWriter fw = new FileWriter("abc.txt");


Lets implement these examples on Eclipse IDE by editing the Java Classes created in our previous posts -

1. Launch Eclipse IDE, open the existing Java Class 'FileWriterDemo.java' from the 'Project 51' as shown below -



2. Reduce the above two lines in the program to a single line as shown below (i.e. by creating the File Class object inside the FileWriter Class object creation statement) -


3. Save & Run the program and observe that the 'How are you doing today?' text got added to the 'abc.txt' file in our project workspace as shown below -



4. Now lets modify the statement as shown below (i.e. by just mentioning the file path directly in the FileWriter Class object creation statement instead of creating a File Class object for the file)  -



5. Remove the text from the 'abc.txt' file in our project workspace as shown below -



6. Save & Run the program and observe that the text 'How are you doing today?' got added to the 'abc.txt' file in our project workspace as shown below -



Hence we can reduce the object creation statements to a single sentence in any of the above two methods (i.e. which ever is applicable for the situation). Some time, when need to perform operations on the File Class object, we may not be able to reduce the sentences. to one sentence.







Please comment below to feedback or ask questions.

Using PrintStream I/0 Class will be explained in the next post.





269. Using FileOutputStream I/O Class









FileOutputStream is used to write text into any file using the bytes values instead of characters as shown in the below example -

Example -

File file1 = new File("abc.txt");

FileOutputStream fos = new FileOutputStream(file1);

byte b[] = {65, 66, 67, 68, 69, 70};   //Providing byte values instead of characters for writing

fos.write(b);

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'FileOutputStreamDemo.java' with main( ) method in the existing Java Project 'Project 51' as shown below -


2. Create a File Class object for 'abc.txt' file in our project workspace as shown below (Resolve import error) -



3. Create a FileOutputStream I/O Class object for the above file file1 as shown below -



4. Resolve the error by selecting 'Adding throws declaration' from the error message as shown below (I am adding throws for explanation purpose, please use try catch instead) -



5. Observe that throws with FileNotFoundException Class got added to the method declaration and the error got resolved as shown below -



6. Create a byte array say b[] and assign the byte values 65,66,67,68,69 & 70 to the byte array as shown below -


7. Pass the byte array variable 'b' to the write( ) method of FileOutputStream Class as shown below (write( ) method will take byte values as input from byte array variable 'b' and convert the values to characters automatically and write the converted characters to the specified text file 'abc.txt' ) -


8. Resolve the error by selecting 'Add throws declaration' option from the error message as shown below -


9. Observe that the throws statement with IOException Class got added to the method declaration and the error got resolved as shown below -


10. Close the FileOutputStream object fos to resolve the warning messages as shown below -



11. Save & Run the program and observe the byte values got converted to ABCDEF and written into the file 'abc.txt' file by the write( ) method as shown below -



Hence we have used write( ) method of FileOutputStream I/O Class to write text into a file using the entered byte values.






Please comment below to feedback or ask questions.

Reducing the object creation statements will be explained in the next post.





268. Using FileInputStream I/O Class









Pre-requisite -



FileInputStream I/O Class need to be used only when we are not able to read any file using FileReader Class. So if not possible to read a file using FileReader Class, then go for FileInputStream Class.

As already explained you in my earlier posts that, the FileReader Class can be used to read text files without images etc, where as FileInputStream can read the files having images etc also. So if your files only contains text go with FileReader Class else go for FileInputStream Class.


Implementation of FileInputStream is similar to the FileReader Class. So instead of newly creating a class for FileInputStream implementation, lets edit the existing java class 'FileReaderDemo.java' in our existing Java Project 'Project 51' as explained in the below steps -

1. Launch Eclipse IDE, open the existing Java Class 'FileReaderDemo.java' from the 'Project 51' Java Project as shown below -



2. Replace above yellow highlighted FileReader Classes with FileInputStream Classes as shown below (Resolve import error)  -



3. Save & Run the program and observe that the text inside the 'xyz.txt' file got displayed in the output as shown below -



Hence the implementation part of FileInputStream I/O Class is same as FileReader I/O Class.







Please comment below to feedback or ask questions.

Using FileOutputStream I/O Class will be explained in the next post.









267. Append text in a file using append( ) method of FileWriter Class









Using append( ) method of FileWriter I/O Class, we can append our text to the existing text written by the write( ) method in the file.

append( ) method can directly append our text to the existing text (written by write( ) method) of the file as shown in the below example -

Example -


File file1 = new File("def.txt");

FileWriter fw = new FileWriter(file1);

fw.write("How are you doing today?");  //Text written by the write( ) method into file

fw.append(" I am doing good.");  //Appends the text to the text written by write( ) method

Output (Inside text file) -

How are you doing today? I am doing good

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'AppendMethodDemo.java' with main( ) method in the existing Java Project 'Project 51' as shown below -



2. Go to our Project Workspace, create an empty file 'def.txt' as shown below -



3. In Eclipse IDE -> 'AppendMethodDemo.java' Class, create a File Class object for the file 'def.txt' created in our project workspace as shown below (Resolve import error) -


4. Create a FileWriter Class object for the above file file1 as shown below (Resolve import error) -


5. View the error and select 'Add throws declaration' option from the error message (I am adding throws for explanation purpose, in reality we should add try/catch to resolve the error) -



6. Observe that IOException Class got added to the method declaration and the error got resolved as shown below -




7. Use write( ) method of FileWriter Class to write the text 'How are you doing today?' into the text file 'def.txt' as shown below -


8. Save & Run the program and observe that the text written by the write( ) method is displayed in the 'def.txt' file as shown below -





8. Now use the append( ) method of FileWriter Class to append the text ' I am doing good.' to the existing text of the 'def.txt' file that is written by write( ) method as shown below -



9. Close the FileWriter object fw to resolve the warning message as shown below -



10. Save & Run the Java Class 'AppendMethodDemo.java' and observe that ' I am doing good.' text got append to the text written by the write( ) method in 'def.txt' file in our project workspace as shown below -




append( ) method appends the new text to the text that was written by the write( ) method. Dont use append( ) method when to want to append your text to the manually written text, as the append( ) will remove the manually written text from the file and add the text specified inside the append( ) method. Hence use append( ) method only to append new text to the existing text that was written by the write( ) method.







Please comment below to feedback or ask questions.

Using FileInputStream I/O Class will be explained in the next post.









266. Writing text to a file using write( ) method of FileWriter I/O Class








Using write( ) method of FileWriter I/O Class, we can write the text into the given text file.

write( ) method can directly write our text into the given text file as shown in the below example -

Example -

File file1 = new File("abc.txt");

FileWriter fw = new FileWriter(file1);

fw.write("How are you doing today?");

Output -

How are you doing today ?

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'FileWriterDemo.java' with main( ) method in the existing Java Project 'Project 51' as shown below -




2. Go to our Project Workspace, create an empty text file 'abc.txt' as shown below -



3. In Eclipse IDE -> 'FileWriterDemo.java' Class, create a File Class object for the created file 'abc.txt' as shown below (Resolve import error) -




4. Create a FileWriter Class object for the above created file file1 as shown below (Resolve import error) -




5. View the error and select 'Add throws declarations' option from the error message as shown below (I am adding throws for explanation purpose, in reality we should add try/catch to resolve the error)  -



6. Observe that IOException Class got added to the method declaration and our error got resolved as shown below -


7. Now use write( ) method of FileWriter Class, to write the given String text to the specified file 'abc.txt' as shown below -



8. Close the object of  FileWriter Class and observe that the above warning message got resolved as shown below -



9. Save & Run the Java Class 'FileWriter.java' , go to the 'abc.txt' in our project location and observe that the text 'How are you doing today?' is displayed as shown below -



Hence we have used write( ) method of FileWriter I/O Class to write text to the text file.








Please comment below to feedback or ask questions.

Append text in a file using append( ) method of FileWriter I/O Class will be explained in the next post.