java files

Read and write file in java

Java read file by char using FileReader

package com.candidjava.file;

import java.io.File;
import java.io.FileReader;

public class FileRead {
	public static void main(String[] args) throws Exception {
		File file=new File("F:/Files/read.txt");
		FileReader fr=new FileReader(file);
	    int i;
		while((i=fr.read())!=-1)
		{
			System.out.println((char)i);
		}
		fr.close();
	}

}

Java read file line by line using BufferedReader

package com.candidjava.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class FileReadByLine {
	public static void main(String[] args)throws Exception {
		File file=new File("F:/Files/read.txt");
		FileReader fr=new FileReader(file);
		BufferedReader br=new BufferedReader(fr);
		String out;
		while((out=br.readLine())!= null)
		{
			System.out.println(out);
		}
		br.close();
		fr.close();
	}

}

Java file to byte array

package com.candidjava.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JavaFileToByteArray {
	public static void main(String[] args) throws Exception {
		File file = new File("F:/Files/sample.mp3");
		FileReader fr = new FileReader(file);
		int i;

		while ((i = fr.read()) != -1) {

			byte b[] = new byte[] { (byte) i };
			for (byte temp : b) {
				System.out.println(temp);
			}
		}

	}
}

How to write to a file in java

package com.candidjava.file;

import java.io.FileWriter;

public class JavaFileWrite {
	public static void main(String[] args)throws Exception {
		FileWriter fw=new FileWriter("F:/Files/write.txt");
		fw.write("Welcome to Candid Java");
		fw.close();
		System.out.println("File write completed successfully");
	}

}

Java append content to file using FileWriter

package com.candidjava.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class JavaFileAppend {
	public static void main(String[] args)throws Exception {
		String data="\n This is end of the File";
		File file=new File("F:/Files/writeLine.txt");
		if(!file.exists())
		{
			file.createNewFile();
		}
		FileWriter fw=new FileWriter(file,true);
		BufferedWriter bw=new BufferedWriter(fw);
		bw.append(data);
		bw.close();
		System.out.println("Appended to file");
		
	}

}

Java write to file line by line

package com.candidjava.file;

import java.io.File;
import java.io.FileWriter;

public class JavaFileWriteLineByLine {
	public static void main(String[] args) throws Exception 
	   {
	      FileWriter fw=new FileWriter("F:/Files/writeLine.txt"); 
	      fw.write("Welcome to Candid java\n");
	      fw.write("Java FileWriter class is used to write character-oriented data to a file.");
	      fw.flush();
	      fw.close();
	      System.out.println("File write completed");
	   }

}

Java byte array to file

package com.candidjava.file;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class JavaByteArrayToFile {
	public static void main(String[] args)throws Exception {
		Path path=Paths.get("F:/Files/writeByte.txt");
		byte b[]=new byte[]{10,20,30,40};
		System.out.println(b);
		Path p1=Files.write(path, b, StandardOpenOption.APPEND);
		System.out.println(p1);
	}

}