ใน Java วิธีอ่านไฟล์ทีละบรรทัดในลำดับย้อนกลับ – บทช่วยสอนที่สมบูรณ์
เผยแพร่แล้ว: 2013-03-30นี่คือโปรแกรม Java ที่ง่ายที่สุดในการอ่านไฟล์ทีละบรรทัดในลำดับย้อนกลับโดยใช้ Java8 API ลองใช้งานและแจ้งให้เราทราบหากคุณพบปัญหาใด ๆ กับสิ่งนี้
- โปรแกรม java เพื่อย้อนกลับเนื้อหาของไฟล์
- เขียนโปรแกรมที่แทนที่แต่ละบรรทัดของไฟล์ด้วยการย้อนกลับ
ฉันจะเขียนชุดสัมภาษณ์ Java ซึ่งคุณจะพบคำถามและคำตอบที่เกี่ยวข้องเพิ่มเติม โปรดคอยติดตามข้อมูลเพิ่มเติม
ในบทช่วยสอนนี้ เราจะดำเนินการตามขั้นตอนด้านล่าง:
- เราจะอ่านไฟล์
crunchify.txt
ซึ่งอยู่ที่เอกสารตำแหน่ง ฉันใช้ Macbook pro ดังนั้นหากคุณใช้ OS และแล็ปท็อป/เดสก์ท็อปที่ต่างกัน โปรดเปลี่ยนเส้นทางของไฟล์ตามนั้นในโปรแกรมCrunchifyReverseLineReader.java
- เราจะอ่านไฟล์แบบ Simple Order ก่อน
- เราจะอ่านไฟล์ใน Reverse Order แล้ว
-
Please note
: โดยค่าเริ่มต้น โปรแกรมด้านล่างจะลบบรรทัดว่างทั้งหมดออกจากเอาต์พุตลำดับย้อนกลับ หากคุณต้องการคงบรรทัดว่างไว้ ให้ดูที่ความคิดเห็นในตัวโปรแกรม - สร้างไฟล์
CrunchifyReverseLineReaderTest.java
และคัดลอกโค้ดจากด้านล่างเพื่อทดสอบผล
รหัสจาวา:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
package crunchify . com . tutorials ; import java . io . ByteArrayOutputStream ; import java . io . File ; import java . io . IOException ; import java . io . RandomAccessFile ; import java . io . UnsupportedEncodingException ; import java . nio . ByteBuffer ; import java . nio . channels . FileChannel ; /** * Read a file from end to start * * @author Crunchify.com */ public class CrunchifyReverseLineReader { private static final int BUFFER_SIZE = 8192 ; private final FileChannel channel ; private final String encoding ; private long filePos ; private ByteBuffer buf ; private int bufPos ; private ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; private RandomAccessFile raf ; private byte lastLineBreak = '\n' ; public CrunchifyReverseLineReader ( File file ) throws IOException { this ( file , null ) ; } public CrunchifyReverseLineReader ( File file , String encoding ) throws IOException { raf = new RandomAccessFile ( file , "r" ) ; channel = raf . getChannel ( ) ; filePos = raf . length ( ) ; this . encoding = encoding ; } public void close ( ) throws IOException { raf . close ( ) ; } public String readLine ( ) throws IOException { byte c ; while ( true ) { if ( bufPos < 0 ) { if ( filePos == 0 ) { if ( baos == null ) { return null ; } String line = bufToString ( ) ; baos = null ; return line ; } long start = Math . max ( filePos - BUFFER_SIZE , 0 ) ; long end = filePos ; long len = end - start ; buf = channel . map ( FileChannel . MapMode . READ_ONLY , start , len ) ; bufPos = ( int ) len ; filePos = start ; // Ignore Empty New Lines c = buf . get ( -- bufPos ) ; if ( c == '\r' | | c == '\n' ) while ( bufPos > 0 && (c == '\r' || c == '\n')) { bufPos--; c = buf . get ( bufPos ) ; } if ( ! ( c == '\r' | | c == '\n' ) ) bufPos ++ ; // IS THE NEW LENE } /* * This will ignore all blank new lines. */ while ( bufPos -- > 0 ) { c = buf . get ( bufPos ) ; if ( c == '\r' | | c == '\n' ) { // skip \r\n while ( bufPos > 0 && (c == '\r' || c == '\n')) { c = buf.get(--bufPos); } // restore cursor if ( ! ( c == '\r' | | c == '\n' ) ) bufPos ++ ; // IS THE NEW Line return bufToString ( ) ; } baos . write ( c ) ; } /* * If you don't want to ignore new line and would like * to print new line too then use below code * and comment out above while loop while (bufPos-- > 0) { byte c1 = buf.get(bufPos); if (c1 == '\r' || c1 == '\n') { if (c1 != lastLineBreak) { lastLineBreak = c1; continue; } lastLineBreak = c1; return bufToString(); } baos.write(c1); } */ } } private String bufToString ( ) throws UnsupportedEncodingException { if ( baos . size ( ) == 0 ) { return "" ; } byte [ ] bytes = baos . toByteArray ( ) ; for ( int i = 0 ; i < bytes . length / 2 ; i ++ ) { byte t = bytes [ i ] ; bytes [ i ] = bytes [ bytes . length - i - 1 ] ; bytes [ bytes . length - i - 1 ] = t ; } baos . reset ( ) ; if ( encoding ! = null ) return new String ( bytes , encoding ) ; else return new String ( bytes ) ; } } |
อัปเดตโค้ดเพื่อลบบรรทัดว่างออกจากไฟล์ตามความคิดเห็นจาก Francesco

บทความอื่นที่ต้องอ่าน: https://crunchify.com/why-and-for-what-should-i-use-enum-java-enum-examples/
ทีนี้มาทดสอบกัน สร้างไฟล์ทดสอบ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package crunchify . com . tutorials ; import java . io . BufferedReader ; import java . io . File ; import java . io . FileReader ; import java . io . IOException ; /** * @author Crunchify.com */ public class CrunchifyReverseLineReaderTest { @SuppressWarnings ( "resource" ) public static void main ( String [ ] args ) throws IOException { /* * If you want to have blank file and would like to add some lines then use below code * * File file = new File("/Users/appshah/Documents/crunchify.txt"); RandomAccessFile raf = new RandomAccessFile(file, * "rw"); for (int i = 0; i <= 10; i++) { // Adding line from 1 to 10 //raf.writeBytes("Adding Line " + i + "\n"); } * raf.close(); * */ /* * Test - 1: In this tutorial we will read exsiting file crunchify.txt * Reading file in Simple Order */ FileReader logReader = new FileReader ( "/Users/appshah/Documents/crunchify.txt" ) ; BufferedReader buffer = new BufferedReader ( logReader ) ; System . out . println ( "~~~~~~~~~~~~~~~~~~~Simple way to read file in Java without Reversing ~~~~~~~~~~~~~~~~~~\n" ) ; for ( String line1 = buffer . readLine ( ) ; line1 ! = null ; line1 = buffer . readLine ( ) ) { System . out . println ( line1 ) ; } /* * Test - 2: In this tutorial we will read exsiting file crunchify.txt * Reading file in Reverse Order */ File file = new File ( "/Users/appshah/Documents/crunchify.txt" ) ; CrunchifyReverseLineReader reader = new CrunchifyReverseLineReader ( file , "UTF-8" ) ; String line ; System . out . print ( "\n~~~~~~~~~~~~~~~~~~~ Reading a file in Reverse Order ~~~~~~~~~~~~~~~~~~~ \n\n" ) ; while ( ( line = reader . readLine ( ) ) ! = null ) { System . out . println ( line ) ; } } } |
Note:
ไฟล์ crunchify.txt มีtotal 11 lines
รวมถึง 1 บรรทัดว่างระหว่างที่คุณเห็นในผลลัพธ์ด้านล่าง
เอาท์พุท:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Simple way to read file in Java without Reversing ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ —————— Crunchify Tutorials Start —————— How To Troubleshoot Higher CPU , Memory & Process Usage ? Top 7 Tips To Speed Up WordPress & Boost Performance Detailed Guide On How To Install SSL On WordPress Site List Of All Genesis WordPress Framework Tips Simplest Hello World Spring MVC Tutorial And JDBC MySQL Tutorial NEW Start Embedded HTTP Jersey Server During Java Application Startup NEWTop 10 Java Interview Questions Answers Java Properties File : How To Read Config . Properties Values In Java ? —————— Crunchify Tutorials End —————— ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Reading a file in Reverse Order ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ —————— Crunchify Tutorials End —————— Java Properties File : How To Read Config . Properties Values In Java ? NEWTop 10 Java Interview Questions Answers NEW Start Embedded HTTP Jersey Server During Java Application Startup Simplest Hello World Spring MVC Tutorial And JDBC MySQL Tutorial List Of All Genesis WordPress Framework Tips Detailed Guide On How To Install SSL On WordPress Site Top 7 Tips To Speed Up WordPress & Boost Performance How To Troubleshoot Higher CPU , Memory & Process Usage ? —————— Crunchify Tutorials Start —————— |