3
A
回答
2
我會使用ByteBuffer,可能與內存映射文件。這允許以大或小的字節讀/寫原始類型。該選項最適合固定寬度的二進制數據。
對於固定寬度文本,您可以使用BufferedReader.readLine()
和String.substring(from, to)
來獲取所需的字段。要輸出固定寬度的字段,您可以使用PrintWriter.printf(format, fields ...)
。
1
2
你也可以看看Fixedformat4j:http://fixedformat4j.ancientprogramming.com/
這是該庫的確切目的
1
基於模式的方法:
- JSaPar允許您指定的模式,通過它可以解析或生成固定寬度的文本。也做一些基本的類型檢查和類型轉換。
4
uniVocity-parsers解析/寫入固定寬度輸入(以及CSV和TSV)。它有很多你可以使用的功能。
樣品輸入:
YearMake_Model___________________________________Description_____________________________Price___
1997Ford_E350____________________________________ac, abs, moon___________________________3000.00_
1999ChevyVenture "Extended Edition"______________________________________________________4900.00_
1996Jeep_Grand Cherokee__________________________MUST SELL!
air, moon roof, loaded_______4799.00_
1999ChevyVenture "Extended Edition, Very Large"__________________________________________5000.00_
_________Venture "Extended Edition"______________________________________________________4900.00_
代碼爲:
FixedWidthFieldLengths lengths = new FixedWidthFieldLengths(4, 5, 40, 40, 8);
FixedWidthParserSettings settings = new FixedWidthParserSettings(lengths);
//sets the character used for padding unwritten spaces in the file
settings.getFormat().setPadding('_');
// creates a fixed-width parser with the given settings
FixedWidthParser parser = new FixedWidthParser(settings);
// parses all rows in one go.
List<String[]> allRows = parser.parseAll(new FileReader(yourFile));
輸出:
[Year, Make, Model, Description, Price]
[1997, Ford, E350, ac, abs, moon, 3000.00]
[1999, Chevy, Venture "Extended Edition", null, 4900.00]
[1996, Jeep, Grand Cherokee, MUST SELL!
air, moon roof, loaded, 4799.00]
[1999, Chevy, Venture "Extended Edition, Very Large", null, 5000.00]
[null, null, Venture "Extended Edition", null, 4900.00]
披露:我是這個庫的作者。它是開放源代碼和免費的(Apache V2.0許可證)。
相關問題
- 1. Octave/Matlab - 讀取固定寬度文件
- 2. 固定寬度格
- 3. 閱讀固定格式文本文件
- 4. 如何使用awk解析固定寬度(NACHA)文件格式?
- 5. 爲什麼固定寬度文件格式仍在使用中?
- 6. Pentaho勺 - 驗證固定寬度輸入文件格式
- 7. 保持在格文本固定寬度
- 8. 帶固定寬度列的表格 - 不指定表格寬度
- 9. Java固定字段文件格式
- 10. 從文本文件中讀取多行固定寬度記錄
- 11. 從文本文件中讀取固定寬度記錄
- 12. 從excel中寫入固定寬度的文本文件vba
- 13. CSS - 固定寬度的跨度/每格
- 14. 分析多行固定寬度文件
- 15. 解析固定寬度的文件
- 16. 將固定寬度文件讀入數組
- 17. Python Pandas,將DataFrame寫入固定寬度文件(to_fwf?)
- 18. 在java中編寫一個帶有固定寬度字段的文件
- 19. 固定寬度和高度條碼PDF417格式
- 20. fancybox2固定寬度
- 21. 使用Java和XML模板處理固定寬度文件(JDOM)
- 22. 讀取固定寬度和缺失值的表格數據
- 23. 固定頭/可變寬度的表格
- 24. 維護表格寬度固定
- 25. 3格,一個固定寬度
- 26. 跨度的固定寬度?
- 27. UILabel固定寬度/固定高度獲取文本編號
- 28. 固定寬度div div寬度div?
- 29. 如何使用固定寬度格式化文本時保留空格?
- 30. 固定寬度文件到sql server寬表
也問在這裏:http://stackoverflow.com/questions/7482021/tactics-for-parsing-fixed-width-text-log-in-java – wrschneider 2011-12-29 16:04:34
我結束了使用BeanIO,但感謝您的幫助!讓我指出了正確的方向。 – TyC 2011-12-30 15:59:48