Giter Site home page Giter Site logo

shubh2-0 / file-handling-in-java Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 0.0 45 KB

Discover the Java file handling world with this repository. It features code examples for reading, writing, copying, renaming, and more. Suitable for beginners and experienced developers, it enhances your skills. Embrace the power of Java file handling and elevate your programming journey.

Java 100.00%
create-file file file-handling-in-java java java-8 java-classes methods stream

file-handling-in-java's Introduction

File Handling in Java

πŸ“ A repository showcasing various file handling operations in Java.

Overview

This repository provides examples and code snippets for performing file handling operations in Java. File handling is an essential aspect of many Java applications, allowing you to read, write, copy, and manipulate files on the local file system.

File handling is a critical aspect of any programming language.

File handling refers to manipulating files and directories on the file system. This article will provide an overview of file handling in Java.

java.io.File Class

The File class in Java is the primary way to handle files and directories. It is part of the java.io package, which is used for input and output operations.

The File class has two constructors:
1️⃣ One that takes a String representing the file path
2️⃣ Another File object representing the file path

This program will display all the contents of a given file on the console .

import java.io.*; 
public class ReadFileExample
{ 
  public static void main(String args[])
  { 
    try{
    File file = new File("C:\\Users\\masai\\Desktop\\test.txt");
      FileReader fr=new FileReader(file);
      BufferedReader br = new BufferedReader(fr);
          String s=null;
          while ((s = br.readLine()) != null)
          System.out.println(s);
         }

     catch(Exception ex)

   {

   System.out.println(β€œException=”+ex);

   }
 }
 }

Java program to write the contents into a file :

import java.io.*;
class FileWriterDemo
{ 
    public static void main(String[] args) throws IOException 
    { 
        
        
        try{
           File file = new File("C:\\Users\\sharp\\Desktop\\out.txt"); //file being created if it does not exist.     
           FileWriter fw=new FileWriter(file); 
           fw.write("Content to be written"); 
           fw.close(); 
		   System.out.println("File written"); 
		   }
		   catch(Exception ex)
		   {
		   System.out.println("Exception is"+ex);
		   }
		   
    } 
    ```
}

Contents

The repository includes the following file handling concepts:

  1. πŸ“₯ BufferedInputStream: Demonstrates how to efficiently read data from a file using a buffered input stream.
  2. πŸ“€ BufferedWriter: Shows how to write data to a file using a buffered writer.
  3. πŸ“ FileOperations: Provides various file operations, such as checking file existence, creating a new file, deleting a file, etc.
  4. πŸ“„ FileContentCopy: Illustrates how to copy the contents of one file to another.
  5. πŸ“„ FileMethods: Demonstrates different methods available for working with files, such as getting file size, file last modified timestamp, etc.
  6. πŸ“– FileReading: Shows how to read the contents of a file using various techniques, including FileReader, FileInputStream, etc.
  7. πŸ”„ FileRenaming: Provides examples of renaming files in Java.
  8. ✍️ FileWriting: Illustrates how to write content to a file using different methods, such as FileWriter, FileOutputStream, etc.
  9. πŸ“ ProgramForCreatingFile: Contains a sample program that creates a new file with predefined content.

Getting Started

To explore the examples and run the code in this repository, follow these steps:

  1. Clone the repository to your local machine using the following command:

    git clone https://github.com/Shubh2-0/File-Handling-In-Java.git
  2. Open the cloned directory in your preferred Java IDE.

  3. Navigate to the specific file or example you want to explore.

  4. Compile and run the Java code to see the file handling operations in action.

Contributing

🌟 Star this repository if you find it helpful!


πŸ”— GitHub Repository

πŸ“¬ Contact

If you want to contact me, you can reach me through below handles.

linkedin  mail-me  whatsapp-me 


file-handling-in-java's People

Contributors

shubh2-0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.