NIO Package Example
//Read Data From File Using Nio
=================================================================================
package govind;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ReadDataThroughNioPackage {
public static void main(String[] args) {
Path path = Paths.get("E:/nio.txt");
try {
byte[] bs = Files.readAllBytes(path);
List<String> strings = Files.readAllLines(path);
System.out.println("Read bytes: \n"+new String(bs));
System.out.println("Read lines: \n"+strings);
} catch (Exception e) {
e.printStackTrace();
}
}
}
===============================================================================================
Write Data into a file using Nio
package govind;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class WriteDataInFileUsingNioPackage {
public static void main(String[] args) {
Path path = Paths.get("e:/datawrite.txt");
try {
String str = "Java is More Interesting Progrmming language";
byte[] bs = str.getBytes();
Path writtenFilePath = Files.write(path, bs,StandardOpenOption.APPEND);
System.out.println("Written content in file:\n"+ new String(Files.readAllBytes(writtenFilePath)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
=====================================================================================
read data from one file and copy to another file in java using Nio
package govind;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FileCopyExampleUsingNioPackage {
public static void main(String[] args) {
Path sourcePath = Paths.get("e:/sourcefile.txt");
Path targetPath = Paths.get("e:/targetfile.txt");
try {
Path path = Files.copy(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option
System.out.println("Target file Path : "+path);
System.out.println("Copied Content : \n"+new String(Files.readAllBytes(path)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Read Data From File Using Nio
=================================================================================
package govind;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ReadDataThroughNioPackage {
public static void main(String[] args) {
Path path = Paths.get("E:/nio.txt");
try {
byte[] bs = Files.readAllBytes(path);
List<String> strings = Files.readAllLines(path);
System.out.println("Read bytes: \n"+new String(bs));
System.out.println("Read lines: \n"+strings);
} catch (Exception e) {
e.printStackTrace();
}
}
}
===============================================================================================
Write Data into a file using Nio
package govind;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class WriteDataInFileUsingNioPackage {
public static void main(String[] args) {
Path path = Paths.get("e:/datawrite.txt");
try {
String str = "Java is More Interesting Progrmming language";
byte[] bs = str.getBytes();
Path writtenFilePath = Files.write(path, bs,StandardOpenOption.APPEND);
System.out.println("Written content in file:\n"+ new String(Files.readAllBytes(writtenFilePath)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
=====================================================================================
read data from one file and copy to another file in java using Nio
package govind;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FileCopyExampleUsingNioPackage {
public static void main(String[] args) {
Path sourcePath = Paths.get("e:/sourcefile.txt");
Path targetPath = Paths.get("e:/targetfile.txt");
try {
Path path = Files.copy(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option
System.out.println("Target file Path : "+path);
System.out.println("Copied Content : \n"+new String(Files.readAllBytes(path)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment