Java File Handling
1. This program helps to create a new file , with their inner content:
package com.test.file;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class CreateNewFIleWithContent {
public static void getFileCreated(){
Scanner sc = new Scanner(System.in);
System.out.print(" Enter the file name path : ");
String filePath=sc.nextLine();
filePath= filePath.replace("/","\\");
try {
FileOutputStream fos = new FileOutputStream(filePath, true);
System.out.print(" Enter the content inside the file: ");
String content = sc.nextLine();
fos.write(content.getBytes());
fos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
getFileCreated();
}
}
2. This program will help to copy from one file to another :
package com.test.file;
import java.io.*;
public class FileHandling {
public static void getFileHandles(String inputFile , String outputFile ) throws IOException {
inputFile= inputFile.replace("/","\\");
outputFile=outputFile.replace("/","\\");
File input = new File (""+inputFile+"");
File output = new File (""+outputFile+"");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(input);
fos = new FileOutputStream(output);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int i = 0;
try{
while(((i = fis.read()) != -1)){
fos.write(i);
}
}catch (IOException e) {
e.printStackTrace();
}
finally {
if(fis != null){
fis.close();
} } }
public static void main(String[] args) throws IOException {
getFileHandles("C:/Users/ankur/Desktop/test.pdf","C:/Users/ankur/Desktop/test3.pdf");
}
}
package com.test.file;
import java.io.File;
public class FolderFiles {
public static void getFileFolderInfo(){
String filePath = "/Users/ankur/Desktop";
File file = new File(filePath);
int counter = 0,counter1=0,counter2=0;
File[] fileFolder = file.listFiles();
System.out.println(" the tpotal size :" + fileFolder.length);
for (File f : fileFolder) {
if (f.isFile()) {
counter += 1;
System.out.println(" This is file :" + f.getName() + " the total counter is :" + counter);
} else if (f.isDirectory()) {
counter1 += 1;
System.out.println(" This is Director: " + f.getName() + " " + counter1);
}
else {
counter2 += 1;
System.out.println(" This is something else :" + f.getName() + " " + counter2);
}
}
System.out.println(counter+ " " +
" " +counter1+ " " +counter2);
}
public static void main(String[] args) {
getFileFolderInfo();
}
}
4. This program will help user to read a file and copy it to console and other files too.
package com.test.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileReaderClass {
private static void getFileRead(){
String path = "\\Users\\ankur\\Desktop";
FileInputStream fis= null;
FileOutputStream fos= null;
try{
File file = new File(path+"\\a1.pdf");
File out = new File(path+"\\a2.pdf");
fis = new FileInputStream(file);
fos = new FileOutputStream(out);
int value = 0;
while((value = fis.read())!=-1) {
System.out.print((char) value);
fos.write(value);
}
} catch(Exception e){
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
getFileRead();
}
}
Comments
Post a Comment