1

这是edifile:ISA*00* 00 *02*HMES *ZZ*MGLYNNCO *120321*1220*U*00401*000015676*0*P*:~GS*FA*HMES*MGLYNNCO*20120321*1220*15691*X *004010~ST*997*000015730~AK1*SM*18292~AK2*204*182920001~AK5*A~AK9*A*1*1*1~SE*6*000015730~GE*1*15691~IEA*1 *000015676~

在 Java 中

我有一个需要解析的 EDI 文件。我可以获取文件并将其转换为字符串并使用标记器将其分开,我不确定的问题是每个段都有另一个分隔符如何在段分隔符处将其分开?

public class EdiParserP {

    public static void main(String[] args) {
        //retrieves the file to be read     
        File file = new File(args[0]);

        int ch;
        StringBuffer strContent = new StringBuffer("");
        FileReader fileInSt = null;

        try{
            fileInSt = new FileReader(file);

            while ((ch = fileInSt.read()) != -1)strContent.append((char)ch);
            fileInSt.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println("file" + file.getAbsolutePath()+ "could not be found");
        } 
        catch (IOException ioe) {
            System.out.println("problem reading the file" + ioe);
        }
        System.out.println("File contents:" + "\n" + strContent + "\n");//used to check to see if file is there
        System.out.print("Length of file" +" " + strContent.length()+ "\n"+ "\n");//used to count the length of the file
        String buffFile = strContent.toString();//used to convert bufferstring to string

        //breaks apart the file with the given delimiter
        StringTokenizer st = new StringTokenizer(buffFile , "*");
        while(st.hasMoreTokens())
        {
            String s =st.nextToken();
            System.out.println(s);
        }
    }
}

我想我的第二个问题是如何检索要放入数据库的信息,我知道如何连接到数据库,以及如何插入,我想,我只是不确定如何从这个字符串中提取数据?谢谢您的帮助

4

1 回答 1

0

也许它不会帮助你到最后,但使用 StringTokenizer 类是相当过时的,你应该使用 String 类的简单 split() 方法。

老实说,我不知道你真的想用那个文件做什么。您想再次拆分从 StringTokenizer 类接收到的每个字符串吗?

于 2012-03-27T20:55:10.057 回答