On the Java formatted text file

Fast learning Java a month, and today just to see this piece of stream buffer, exercise encountered a seemingly simple, but I will not question, formatted text file is, in fact, fundamental research, string handling, but I it is the brain turn to turn, however, like a long time, and finally write this crappy code, but finally realized the small features, alone, caught my hair out today, I have to write a note about the record.
java
In order to prepare the code text files; files are upset the order of the sequence number, and a text file does not wrap, i.e. row shows, I want their sorting according to sequence number and the wrap and writes a file to another. Here is my attempt process.
Txt file is a text file on the hard disk, as follows, is a one-line display.

3. Shi Zhong, assistant minister Guo Youzhi, Fei Yi, Dong Yun and so on, euphemistically real good, Zhi Zhong considered pure, simple pull to left is the late emperor His Majesty. Stupid things that the palace, no matter the size, noted the consultative and implementation, must benefit Buque leakage, have Guangyi. 8. Route Expeditionary Chen Yi Xing asked His Majesty is willing complex effect, no effect, then the minister of sin rule to tell the spirit of the late emperor. If there is no Heungdeok words, the responsibility Yau, Yi, Yun and other slow to Chang the blame; His Majesty should be matched self-seeking, consultative Suwa good way, Chana Ya statement, deep chase late emperor Testament, very stipendiary minister grateful . 4. Shu generals are the pet, sexual activity, intelligible military trial in the past, the late emperor Yue can call it, is a rather controversial move pet as Governor. Stupid things that camp, noted the consultative, will make the line array harmony, the pros and cons too. 2. palace Fuchu, both as a whole, Zhi penalty rank, not similarities and differences. If commit crimes and to loyalty of good, should pay Yousi On the punishment reward, whereof Majesty dawn of the reason, not favoritism, different inside and outside the law also. 1. Late emperor did not venture a half and middle Bengcu today under the third, Yizhou Pibi, this sincere despite the critical autumn too. However, guards of the minister in the relentless, Tadashi forget the body on the outside who cover chase late emperor of Shu Yu, quoted in His Majesty also want. Cheng Yi opened St listen to light late emperor left behind Germany, restore Hongzhi Shi of the gas, should not sell ourselves short, allusions of justice, in order to plug loyal remonstrance of the road also. 9. When this away, temporary table Tiling, I do not know said. 6. Commoner, Gonggeng Nanyang, Gouquanxingming in troubled times, no smell of the princes. Late emperor not to Chen dirty, rustic bent from the vain, the three Gu Cao Lu Chen in being, Chen Yi consultative matter when the world, by the gratitude, then Xu Xiandi to Quchi. After overturning value, subject to any on the occasion of the defeated army, ordered in between distress, Seoul to twenty year carry on. 7. The late emperor known minister cautious, so temporary collapse sent Chen Yi event also. Since ordered Suye worry sigh, fear no effect entrusted to injury late emperor of the Ming, Lu crossing it in May, in-depth barren. Today the South has been set, Bing Jia is enough, when the award rate of the armed forces, the North Central Plains, Shu dried dull, throw into confusion eliminating traitors fierce, Xing Fu Han Dynasty, also in the old capital. This minister reported that the late emperor and loyalty so that His Majesty the ministry also. As appropriate, profit and loss, into the faithful words, the Yau, Yi, allowing the office also. The pro-Xian Chen, far villain, this is also the first Chinese and so prosperous; pro-villain, far Xian Chen, Han thereafter so left leaning too. When the late emperor, each with the minister on this matter, not a bad idea sigh hate to Huan, Ling also. Shi Zhong, Shang, Eliot, join the army, the minister noted Liang Zhen dead knots, May the letter of His Majesty pro, the Han Dynasty of the lung, can also be counted and day.

And my goal is output in the following format:

1. Late emperor did not venture a half and middle Bengcu today under the third, Yizhou Pibi, this sincere despite the critical autumn too. However, guards of the minister in the relentless, Tadashi forget the body on the outside who cover chase late emperor of Shu Yu, quoted in His Majesty also want. Cheng Yi opened St listen to light late emperor left behind Germany, restore Hongzhi Shi of the gas, should not sell ourselves short, allusions of justice, in order to plug loyal remonstrance of the road also.
2. palace Fuchu, both as a whole, Zhi penalty rank, not similarities and differences. If commit crimes and to loyalty of good, should pay Yousi On the punishment reward, whereof Majesty dawn of the reason, not favoritism, different inside and outside the law also.
3. Shi Zhong, assistant minister Guo Youzhi, Fei Yi, Dong Yun and so on, euphemistically real good, Zhi Zhong considered pure, simple pull to left is the late emperor His Majesty. Stupid things that the palace, no matter the size, noted the consultative and implementation, must benefit Buque leakage, have Guangyi.
4. Shu generals are the pet, sexual activity, intelligible military trial in the past, the late emperor Yue can call it, is a rather controversial move pet as Governor. Stupid things that camp, noted the consultative, will make the line array harmony, the pros and cons too.
The pro-Xian Chen, far villain, this is also the first Chinese and so prosperous; pro-villain, far Xian Chen, Han thereafter so left leaning too. When the late emperor, each with the minister on this matter, not a bad idea sigh hate to Huan, Ling also. Shi Zhong, Shang, Eliot, join the army, the minister noted Liang Zhen dead knots, May the letter of His Majesty pro, the Han Dynasty of the lung, can also be counted and day.
6. Commoner, Gonggeng Nanyang, Gouquanxingming in troubled times, no smell of the princes. Late emperor not to Chen dirty, rustic bent from the vain, the three Gu Cao Lu Chen in being, Chen Yi consultative matter when the world, by the gratitude, then Xu Xiandi to Quchi. After overturning value, subject to any on the occasion of the defeated army, ordered in between distress, Seoul to twenty year carry on.
7. The late emperor known minister cautious, so temporary collapse sent Chen Yi event also. Since ordered Suye worry sigh, fear no effect entrusted to injury late emperor of the Ming, Lu crossing it in May, in-depth barren. Today the South has been set, Bing Jia is enough, when the award rate of the armed forces, the North Central Plains, Shu dried dull, throw into confusion eliminating traitors fierce, Xing Fu Han Dynasty, also in the old capital. This minister reported that the late emperor and loyalty so that His Majesty the ministry also. As appropriate, profit and loss, into the faithful words, the Yau, Yi, allowing the office also.
8. Route Expeditionary Chen Yi Xing asked His Majesty is willing complex effect, no effect, then the minister of sin rule to tell the spirit of the late emperor. If there is no Heungdeok words, the responsibility Yau, Yi, Yun and other slow to Chang the blame; His Majesty should be matched self-seeking, consultative Suwa good way, Chana Ya statement, deep chase late emperor Testament, very stipendiary minister grateful .
9. When this away, temporary table Tiling, I do not know said.

code show as below

package Practices.File.Demo05;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

public class Demo05readlinegood {
    public static void main(String[] args) throws IOException {
        //创建缓冲字符流对象
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\Java程序练手\\ff.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\Java程序练手\\kk.txt"));
        //创建HashMap集合用来存储格式化后的数据
        HashMap<String,String> maps = new HashMap<>();
        //创建一个ArrayList集合用来存储序号数据
        ArrayList<Character> list = new ArrayList<>();
        //读取目标文件,赋值给一个字符串对象
        String buf = "";
        while ((buf=br.readLine())!=null){
            //把字符串拆分为字符数组
            char[] chars = buf.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                //分析每一个字符,如果是0-9之间的为序号,存储到ArrayList集合中
                if ((int) chars[i]>=48 && (int) chars[i]<=57){
                    list.add(chars[i]);
                }
            }
            //遍历ArrayList集合,使用String方法的IndexOf方法获取索引区间
            String part = "";
            int indexes1 = 0;
            int indexes2 = 0;
            for (int i = 1; i <= list.size()-1; i++) {
                indexes2 = buf.indexOf(list.get(i));
                part = buf.substring(indexes1,indexes2);
                String[] spreads = part.split("\\.");
                maps.put(spreads[0],spreads[1]);
                //System.out.println(part);
                indexes1=indexes2;
            }
            //最后一个单独查找
            part = buf.substring(indexes2);
            maps.put(part.split("\\.")[0],part.split("\\.")[1]);
            for(int j = 1;j<=maps.size();j++) {
                String keys = String.valueOf(j);
                String values = maps.get(keys);
                //写入数据
                bw.write(keys+"."+values);
                bw.newLine();
                System.out.println(keys+"."+values);
            }
        }
        //不要忘记关闭流并释放资源
        bw.close();
        br.close();
    }
}

Code is very bad, the method also stupid, but function is the first step to achieve Well, hope to be able to learn better and more efficient way, bigwigs have any suggestions welcome in the comments section to discuss Oh, hee hee.

Published 22 original articles · won praise 1 · views 1437

Guess you like

Origin blog.csdn.net/weixin_44803446/article/details/104157146