Tag: java files program

  • Java Program to Write Char by Char into a File

    In this tutorial, you will learn how to Write Char by Char into a File in java. To understand it better, you may want to check the following first:


    Java Program to Write Char by Char into a File

    //write char by char into a file in java
    
     import java.io.*;
    
     public class FileByChar 
     {
     
      public static void main(String[] args) throws IOException 
      {
        File f=new File("javaCode.txt"); //Creation of File Descriptor for output file
        FileWriter fw=new FileWriter(f);  //Creation of File Writer object
        fw.write('S');  //Write character by Character
        fw.write('i');
        fw.write('m');
        fw.write('p');
        fw.write('l');
        fw.write('e');
        fw.write('2');
        fw.write('C');
        fw.write('o');
        fw.write('d');
        fw.write('e');
        fw.flush();
        fw.close();
          
      System.out.println("Check the javaCode.txt file");
      }
     
     }

    Output:

    Check the javaCode.txt file

    Open javaCode.txt file that will be created after execution of a program

    javaCode.txt

    Simple2Code


  • Java Program to Write in a File Line by Line

    In this tutorial, you will learn how to Write in a File Line by Line in java. To understand it better, you may want to check the following first:


    Java Program to Write in a File Line by Line

    //write in a file line by line 
    
     import java.io.*;  
    
     public class FileWrite
     {
     
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt"); 
        
        FileWriter fw = new FileWriter(f);
        
        fw.write("Welcome to Simple2Code. \n"); 
        fw.write("Start the Tutorial");
        fw.flush();
        fw.close();
        System.out.println("Done..Check javaCode.txt");
      }
     
     }

    Output:

    Done..Check javaCode.txt

    javaCode.txt

    Welcome to Simple2Code.
    Start the Tutorial


  • Java Program to Read in a File Line by Line

    In this tutorial, you will learn how to Read in a File Line by Line in java. To understand it better, you may want to check the following first:


    Java Program to Read in a File Line by Line

    First, create a text file named javaCode.txt and write something on that text file. And then Save that text file within the same directory as the source code. For example, the following content is present in the javaCode.txt file.

    javaCode.txt

    Welcome to Simple2Code.
    Let Start
    first tutorial
    second Program

    Source Code:

    //read in a file line by line 
    
     import java.io.*; 
     
     public class FileRead
     {
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt");     
        FileReader fr = new FileReader(f);  
    
        BufferedReader br = new BufferedReader(fr);  
        String s = null;
    
        while((s = br.readLine()) != null)
        {
          System.out.println(s);
              
        }
          fr.close();
      }
     
     }

    Output:

    Welcome to Simple2Code.
    Let Start


  • Java Program to Search a Particular Word in a File

    In this tutorial, you will learn how to Search for a Particular Word in a File in java. To understand it better, you may want to check the following first:


    Java Program to Search a Particular Word in a File

    First, save the content in a text file with the same name that you enter in a program. For this example, a text file saved with the name javaCode.txt

    javaCode.txt

    Welcome to Simple2Code.
    Let Start
    first tutorial
    second Program

    Source Code:

    //search the particular word in a file 
    
     import java.io.*;
    
     public class SearchWord
     {
      public static void main(String[] args) throws IOException 
      {
          File f = new File("javaCode.txt"); //file
          String[] words=null;  //Intialization
          
          FileReader fr = new FileReader(f);  //File Reader object
          BufferedReader br = new BufferedReader(fr); 
          
          String s;     
          String input="program";
          int count=0;   //Intializing count
          
          while((s = br.readLine())!=null) //Reading Content from the file
          {
            words=s.split(" ");  //Split the word using space
              for (String word : words) 
              {
                if (word.equals(input))  //Search for the given word
                {
                  count++;    //If Present increase the count by one
                }
              }
          }
          if(count!=0)  //Check for count not equal to zero
          {
            System.out.println("Number of times given word present: "+count);
          }
          else
          {
            System.out.println("The given word is not present.");
          }
          
            fr.close();
      }
     }

    Output:

    Number of times given word present: 3


  • Java Program to Read Char by Char from a File

    In this tutorial, you will learn how to Read Char by Char from a File in java. To understand it better, you may want to check the following first:


    Java Program to Read Char by Char from a File

    First, save the content in a text file with the same name that you enter in a program.
    For this example, a text file saved with the name javaCode.txt

    javaCode.txt

    Welcome to Simple2Code program

    //read char by char from a file 
    
     import java.io.*;
    
     public class ReadCharByChar
     {
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt");   
        
        FileReader fr=new FileReader(f);  
        BufferedReader br=new BufferedReader(fr);
          
        int c = 0;             
        
        while((c = br.read()) != -1)  //Read char by Char
        {
          char character = (char) c;    //converting integer to char
          System.out.println(character); //Display
        }
          
      }
     }

    Output:

     W
     e
     l
     c
     o
     m
     e
    
     t
     o
    
     S
     i
     m
     p
     l
     e
     2
     C
     o
     d
     e
    
     p
     r
     o
     g
     r
     a
     m

  • Java Program to Count the Number of Words in a File

    In this tutorial, you will learn how to Count the Number of Words in a File in java. To understand it better, you may want to check the following first:


    Java Program to Count the Number of Words in a File

    First, save the content in a text file with the same name that you enter in a program.
    For this example, a text file saved with the name javaCode.txt

    javaCode.txt

    Welcome to Simple2Code.
    Let Start
    first tutorial
    second Program

    //count number of words in a file 
    
     import java.io.*;
    
     public class WordCount
     {
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt"); //creating txt file
        
        String[] words=null;  //Intializing
        
        int wordCount = 0;     //Intializing word count 
        FileReader fr = new FileReader(f);    //Creation of File Reader object
        BufferedReader br = new BufferedReader(fr);  //Creation of BufferedReader object
        String s;
        while((s = br.readLine())!=null)   //Reading Content from the file
        {
          words = s.split(" ");   //Split the word using space
          wordCount = wordCount + words.length;   //increment in word count for each word
        }
        fr.close();
        System.out.println("Number of words in the file:" +wordCount);  //Display
      }
     }

    Output:

    Number of words in the file:9


  • Java Program to Count the Number of Lines in a File

    In this tutorial, you will learn how to Count the Number of Lines in a File in java. To understand it better, you may want to check the following first:


    Java Program to Count the Number of Lines in a File

    First, save the content in a text file with the same name that you enter in a program.
    For this example, a text file saved with the name javaCode.txt

    javaCode.txt

    Welcome to Simple2Code.
    Let Start
    first tutorial
    second Program

    Source Code:

    //count number of lines in a file
    
     import java.io.BufferedReader;
     import java.io.File;
     import java.io.FileReader;
     import java.io.IOException;
     
     public class LineCount 
     {
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt");
        int count=0;   
            
        FileReader fr = new FileReader(f);  
        BufferedReader br = new BufferedReader(fr); 
        
        String s;         
            
        while((s=br.readLine())!=null) //Reading Content 
        {
          count++; //increases for each line
        }
        fr.close();
        //Display number of line
        System.out.println("Total number of line:"+count); 
      }
     
     }

    Output:

    Total number of line: 4


  • Java Program to Copy Content from one File to another File

    In this tutorial, you will learn how to copy the content of one file to another in java. Here the new file is created where the content of another file is copied to this newly created file.


    Java Program to Copy Content from one File to another File

    First, save the content in a text file with the same name that you enter in a program.
    For this example, a text file saved with the name javaCode.txt

    javaCode.txt

    Welcome to Simple2Code.
    Let Start
    first tutorial
    second Program

    And after the execution new file will be created as named copiedJavaCode.txt. And the content will be copied to this text file.

    Source Code:

     //copy content from one to another file 
    
     import java.io.*;
    
     public class CopyFile
     {
        public static void main(String[] args) throws IOException 
      {
          File f1 = new File("javaCode.txt");      //saved file
          File f2 = new File("copiedJavaCode.txt");   //destined file
          
          FileReader fr = new FileReader(f1);      //file reader object 
          BufferedReader br=new BufferedReader(fr);  //buffer reader object
          FileWriter fw= new FileWriter(f2); //file writer object 
          
          String s=null;
          
          while((s = br.readLine())!=null) //Copying Content to the new file
          {
            fw.write(s);
            fw.write("\n");
            fw.flush();
          }
            fw.close();
            
          System.out.println("Successfully Copied.");
          System.out.println("Check the copiedJavaCode.txt.");
        }
     
     }

    Output: After execution check your copiedJavaCode.txt file where all the content is copied from javaCode.txt file.

    Successfully Copied.
    Check the copiedJavaCode.txt.


  • Java Program to Check Whether a New file is Created or Already Exists

    In this tutorial, we will write a program to check whether a new file is created or already exists in java. This java program check if the file is already created in that particular directory or it is already present.

    The program also uses try and catch for exception handling. If you want to learn about Java files and Exceptions, click the link below.


    Java Program to Check Whether a New file is Created or Already Exists

    //check whether new file is created or already exists 
    
     import java.io.*;  
     public class FileCreated 
     {  
      public static void main(String[] args) 
      {  
    
        try {  
          File file = new File("javaCode.txt");  
          if (file.createNewFile()) {  
              System.out.println("New File is created.");  
          } else {  
              System.out.println("File already exists.");  
          }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    
      }  
     }

    Output:

    New File is created.

    A new file will be created in the same directory.


  • Java Program to Append Content into a File

    This tutorial shows you how to append Content into a file in java.

    Append means to add something at the end. Similarly, the following java program adds new Content into a file. File in-built function append() is used to add new content at the end.

    If you do not about the file system in java then click the link below.


    Program to Append Content into a File in Java

     //append content into a file 
    
     import java.io.File;
     import java.io.FileWriter;
     import java.io.IOException;
     
     public class FileAppend
     {
      public static void main(String[] args) throws IOException 
      {
        File f = new File("javaCode.txt");     
        FileWriter fw = new FileWriter(f,true);  //Create filewriter object
    
        fw.append("Welcome to Simple2Code\n");  //Append the Content 
    
        fw.flush();          
        fw.close();
    
        System.out.println("Check javaCode.txt.")
      }
     
     }

    Output:

    Check javaCode.txt.

    Open the file that will be created in the same directory after execution.
    javaCode.txt

    Welcome to Simple2Code.