How to Read Streaming Data From a File in Java

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • 60 minutes Interview Questions
  • Computer Glossary
  • Who is Who

How to read data from one file and print to some other file in Java?


Coffee provides I/O Streams to read and write data where a Stream represents an input source or an output destination which could be a file, i/o devise, other programs, etc.

In general, a Stream will be an input stream or, an output stream.

  • InputStream − This is used to read data from a source.

  • OutputStream − This is used to write data to a destination.

Based on the data they handle there are ii types of streams −

  • Byte Streams − These handle data in bytes (8 bits) i.east., the byte stream classes read/write data of 8 $.25. Using these you lot tin can store characters, videos, audios, images, etc.

  • Grapheme Streams − These handle data in 16 bit Unicode. Using these you can read and write text information but.

The following diagram illustrates all the input and output Streams (classes) in Java.

Among these, you can read the contents of a file using Scanner, BufferedReader and, FileReader classes.

In the same way, you can write data into a file using BufferedWriter, FileOutputStream, FileWriter.

Writing contents of one file to other

Following is a Coffee program that reads information from a file to a Cord using the Scanner class and writes it to some other file using the FileWriter class.

Example

import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public grade CopyContent {    public static void chief(String[] args) throws IOException {       //Instantiating a file grade       File file = new File("D:\\sampleData.txt");       //Instantiate an FileInputStream course       FileInputStream inputStream = new FileInputStream(file);       //Instantiating the Scanner grade       Scanner sc = new Scanner(inputStream);       //StringBuffer to store the contents       StringBuffer buffer = new StringBuffer();       //Appending each line to the buffer       while(sc.hasNext()) {          buffer.append(" "+sc.nextLine());       }       System.out.println("Contents of the file: "+buffer);       //Creating a File object to hold the destination file       File dest = new File("D:\\outputFile.txt");       //Instantiating an FileWriter object       FileWriter writer = new FileWriter(dest);       //Writing content to the destination       writer.write(buffer.toString());       writer.flush();       Arrangement.out.println("File copied successfully.......");    } }

Output

Contents of the file: Tutorials Point originated from the idea that there exists a  class of readers who respond better to online content and prefer to learn new skills  at their own pace from the comforts of their cartoon rooms. The journey commenced  with a single tutorial on HTML in 2006 and elated by the response it generated,  we worked our fashion to adding fresh tutorials to our repository which now proudly  flaunts a wealth of tutorials and allied articles on topics ranging from programming  languages to spider web designing to academics and much more. 40 1000000 readers read 100  meg pages every calendar month. Our content and resource are freely available and we prefer  to continue it that way to encourage our readers learn as many skills as they would like to.  We don't force our readers to sign up with us or submit their details either.  No preconditions and no impediments. Simply Easy Learning! File copied successfully.......

raja

Published on 10-Sep-2019 12:38:xl

  • Related Questions & Answers
  • How to read data from .csv file in Java?
  • How to read the data from a file in Java?
  • How to read/write information from/to .backdrop file in Java?
  • How to read the data from a properties file in Coffee?
  • How to read the data from a CSV file in Java?
  • How to read data from PDF file and brandish on console in Java?
  • How to read information in from a file to String using coffee?
  • Moving a file from i directory to another using Java
  • How to read information from *.CSV file using JavaScript?
  • How to read information from a file using FileInputStream?
  • How to write a program to copy characters from one file to another in Coffee?
  • How to move a file from one binder to another using Python?
  • Write a C programme to read a data from file and display
  • C program to re-create the contents of one file to another file?
  • Read Data from a Text File using C++

glattspeargons.blogspot.com

Source: https://www.tutorialspoint.com/how-to-read-data-from-one-file-and-print-to-another-file-in-java

0 Response to "How to Read Streaming Data From a File in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel