Java Read file วิธีอ่าน File ด้วย BufferedReader แบบต่างๆ

Java Read file ผ่าน BufferedReader แบบง่ายๆ เลยก็คือ

BufferedReader bf = new BufferedReader(new FileReader("C:/testdata.csv"))

วิธีนี้จะเจอปัญหาว่า กรณีเป็นภาษาไทย จะได้เป็นภาษาต่างดาว และถ้าเราอยากจะเพิ่ม CharSet พวก UTF8 , TIS620 พวกนี้ต้องทำการส่งเพิ่มเข้าไปแบบนี้ครับ

BufferedReader bf = new BufferedReader(new FileReader("C:/testdata.csv"), StandardCharsets.UTF_8)

 คุ้นๆว่า Java Version เก่าๆ จะไม่รองรับนะ เพราะ constructor ของ BufferedReader จะไม่มีให้ส่ง CharSet เข้าไป

ถ้า Java Version เก่าๆ หน่อยให้ใช้คำสั่งนี้

BufferedReader bf =new BufferedReader(new InputStreamReader(new FileInputStream("C://test.csv"), StandardCharsets.UTF_8)) 

เรียกได้ว่ายาวเป็นวา หรือจะให้สั้นหน่อยก็ใช้แบบนี้ครับ JDK 1.7 น่าจะใช้ได้อยู่นะแต่เก่ากว่านี้ไม่แน่ใจ

BufferedReader bf =Files.newBufferedReader(Paths.get("C://test.csv"), StandardCharsets.UTF_8)

ลองใช้งานกันดูนะครับ ส่วนขั้นตอนหลังจากอ่าน File ได้แล้วก็จะเอาไป วน loop ออกหรือเอาไปโยนเข้าคำสั่ง copy อะไรก็ว่ากันไปครับ

คำสั่งที่ใช้บ่อยเกี่ยวกับ Java Read File

File file = new File("path/file.txt");
Path path = Paths.get("path/file.txt");

File file = new File("path", "test.txt");
Path path = Paths.get("path", "test.txt");

Path path = Paths.get("path").resolve("test.txt");


File file = new File("path/file.txt");
boolean result = file.delete();

Path path = Paths.get("path/file.txt");
Files.delete(path);

Path path = file.toPath();
File file = path.toFile();
//Create File
boolean result = file.createNewFile();
Path newPath = Files.createFile(path);
//Create Directory
boolean result = file.mkdir();
File newPath = Files.createDirectory(path);
//move file , reaname
boolean result = file.renameTo(new File("path/test2.txt"));
Path newPath = Files.move(path, Paths.get("path/test2.txt"));
//delete file
boolean result = file.delete();
Files.delete(Paths.get(path));
//read metadata
boolean fileExists = file.exists();
boolean fileIsFile = file.isFile();
boolean fileIsDir = file.isDirectory();
boolean fileReadable = file.canRead();
boolean fileWritable = file.canWrite();
boolean fileExecutable = file.canExecute();
boolean fileHidden = file.isHidden();

//java.nio
boolean pathExists = Files.exists(path);
boolean pathIsFile = Files.isRegularFile(path);
boolean pathIsDir = Files.isDirectory(path);
boolean pathReadable = Files.isReadable(path);
boolean pathWritable = Files.isWritable(path);
boolean pathExecutable = Files.isExecutable(path);
boolean pathHidden = Files.isHidden(path);

// java.io API
String absolutePathStr = file.getAbsolutePath();
String canonicalPathStr = file.getCanonicalPath();

// java.nio API
Path absolutePath = path.toAbsolutePath();
Path canonicalPath = path.toRealPath().normalize();

URI fileUri = file.toURI();
URI pathUri = path.toUri();

// java.io API
String[] list = file.list();
File[] files = file.listFiles();

// java.nio API
DirectoryStream<Path> paths = Files.newDirectoryStream(path);




ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Oracle date format จัด format date ให้แสดง พศ และ เดือน ภาษาไทยหรือตามภาษาที่เราเลือก

java -Xms , java -Xmx กำหมด memory ให้ JVM เพื่อป้องกันปัญหา Out of Memory

Java this กับ super การใช้งานคำสั่ง this กับ super ใน ภาษา Java