J2ME中解析txt文件中的数据

发布于 2024-07-11 10:23:54 字数 1318 浏览 11 评论 0原文

基本上我正在 J2ME 中创建一个室内导航系统。 我已将位置详细信息放入 .txt 文件中,即

  • 地点名称及其坐标。
  • 具有各自起始节点和结束节点的边以及权重(节点的长度)。

    我将这两个详细信息放在同一个文件中,这样用户就不必下载多个文件来使地图正常工作(这可能会变得耗时且看起来很复杂)。 所以我所做的就是首先输入位置名称和坐标来分隔不同的细节,然后我通过画一条带有多个下划线的线将该部分与下一部分(即边缘)分开。

    现在我遇到的问题是通过设置命令(同时手动标记输入流)将不同的详细信息解析为单独的数组,以检查下一个标记是否是下划线。

  • 如果是,(用伪代码术语),移动到流中的下一行,创建一个新数组并用下一组详细信息填充它。

    我找到了一些解释/代码这里< /a> 执行类似的操作,但仍然解析为一个数组,尽管它手动标记输入。 关于该做什么有什么想法吗? 谢谢

    文本文件说明
    文本具有以下格式...

    <--第 1 部分-->
    /**
      * 第一节的格式如下
      * x坐标;y坐标;位置名称
      */

    12;13;纽约市
    40;12;华盛顿特区
    ...等等

    ________________________ <--(下划线分隔符)

    <--第2节-->
    /**
      * 它实际上是一个邻接列表,但间接提供“边缘”详细信息。
      * 它的形式是这样的
      * StartNode/MainReferencePoint;Endnode1;distance2endNode1;Endnode2;distance2endNode2;...等
      */

    费城;华盛顿特区;7;纽约市;2
    纽约市;佛罗里达州;24;伊利诺伊州;71
    ...等等

  • Basically I'm creating an indoor navigation system in J2ME. I've put the location details in a .txt file i.e.

  • Locations names and their coordinates.
  • Edges with respective start node and end node as well as the weight (length of the node).

    I put both details in the same file so users dont have to download multiple files to get their map working (it could become time consuming and seem complex).
    So what i did is to seperate the deferent details by typing out location Names and coordinates first, After that I seperated that section from the next section which is the edges by drawing a line with multiple underscores.

    Now the problem I'm having is parsing the different details into seperate arrays by setting up a command (while manually tokenizing the input stream) to check wether the the next token is an underscore.

  • If it is, (in pseudocode terms), move to the next line in the stream, create a new array and fill it up with the next set of details.

    I found a some explanation/code HERE that does something similar but still parses into one array, although it manually tokenizes the input. Any ideas on what to do? Thanks

    Text File Explanation
    The text has the following format...

    <--1stSection-->
     /**
      * Section one has the following format
      * xCoordinate;yCoordinate;LocationName
      */

    12;13;New York City
    40;12;Washington D.C.
    ...e.t.c

    _________________________  <--(underscore divider)

    <--2ndSection-->
     /**
      * Its actually an adjacency list but indirectly provides "edge" details.
      * Its in this form
      * StartNode/MainReferencePoint;Endnode1;distance2endNode1;Endnode2;distance2endNode2;...e.t.c
      */

    philadelphia;Washington D.C.;7;New York City;2
    New York City;Florida;24;Illinois;71
    ...e.t.c

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    往日情怀 2024-07-18 10:23:54
    package filereader;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Hashtable;
    import java.util.Vector;
    
    public class FileReader {
        String locationSection;
        String edgeSection;
        Vector locations;
        Vector edges;
    
        public FileReader(String fileName) {
            // read the contents into the string
            InputStream is = getClass().getResourceAsStream(fileName);
            StringBuffer sb = new StringBuffer();
            int ch;
            try {
                while ((ch = is.read()) != -1) {
                    sb.append((char) ch);
                }
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String text = sb.toString();
    
            // separate locations and edges
            String separator = "_________________________";
    
            // read location section, without last end-of-line char
            int endLocationSection = text.indexOf(separator) - 1;
            locationSection = text.substring(0, endLocationSection);
    
            // read edges section, without end-of-line char after separator
            int startEdgeSection = endLocationSection + separator.length() + 3;
            edgeSection = text.substring(startEdgeSection, text.length());
    
            // parse locations and edges
            locations = getLocationsVector(locationSection);
            edges = getEdgesVector(edgeSection);
        }
    
        // parse locations section
        public Vector getLocationsVector(String section) {
            Vector result = new Vector();
            int startLine = 0;
            int endLine = section.indexOf('\n');
            while (endLine != -1) {
                String line = section.substring(startLine, endLine);
                result.addElement(parseLocationsLine(line, ';'));
                startLine = endLine + 1;
                if (endLine == section.length() - 1)
                    break;
                endLine = section.indexOf('\n', startLine);
                // if no new line found, read to the end of string
                endLine = (-1 == endLine) ? section.length() - 1 : endLine;
            }
            return result;
        }
    
        // parse edges section
        public Vector getEdgesVector(String section) {
            Vector result = new Vector();
            int startLine = 0;
            int endLine = section.indexOf('\n');
            while (endLine != -1) {
                String line = section.substring(startLine, endLine - 1);
                result.addElement(parseEdgesLine(line, ';'));
                startLine = endLine + 1;
                if (endLine == section.length() + 1)
                    break;
                endLine = section.indexOf('\n', startLine);
                // if no new line found, read to the end of string
                endLine = (-1 == endLine) ? section.length() + 1 : endLine;
            }
            return result;
        }
    
        // parse locations line
        public Hashtable parseLocationsLine(String value, char splitBy) {
            Hashtable result = new Hashtable();
            int xCEnd = value.indexOf(splitBy);
            int yCEnd = value.indexOf(splitBy, xCEnd + 1);
            result.put("x", value.substring(0, xCEnd));
            result.put("y", value.substring(xCEnd + 1, yCEnd));
            result.put("location", value.substring(yCEnd + 1, 
                value.length() - 1));
            return result;
        }
    
        // parse edges line
        public Hashtable parseEdgesLine(String value, char splitBy) {
            Hashtable result = new Hashtable();
            int snEnd = value.indexOf(splitBy);
            result.put("startnode", value.substring(0, snEnd));
            int n = 1;
            int start = snEnd + 1;
            int enEnd = value.indexOf(splitBy, snEnd + 1);
            int dstEnd = value.indexOf(splitBy, enEnd + 1);
            while (enEnd != -1 && dstEnd != -1) {
                result.put("endnode" + String.valueOf(n), 
                        value.substring(start, enEnd));
                result.put("distance" + String.valueOf(n), value.substring(
                        enEnd + 1, dstEnd));
                start = dstEnd + 1;
                enEnd = value.indexOf(splitBy, start);
                if (dstEnd == value.length())
                    break;
                dstEnd = value.indexOf(splitBy, enEnd + 1);
                // if last endnode-distance pair, read to the end of line
                dstEnd = (-1 == dstEnd) ? value.length() : dstEnd;
                n++;
            }
            return result;
        }
    
        // getters for locations and edges
        public Vector getLocations() {
            return locations;
        }
    
        public Vector getEdges() {
            return edges;
        }
    
    }
    

    在应用程序屏幕中的某个位置:

    fr = new FileReader("/map.txt");
    Vector vct1 = fr.getLocations();
    for (int i = 0; i < vct1.size(); i++) {
        Hashtable location = (Hashtable) vct1.elementAt(i);
        Enumeration en = location.keys();
        String fv = "";
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = (String)location.get(key);
            fv = fv + value + "-";
        }
        this.add(new LabelField(fv));       
    
    }
    Vector vct2 = fr.getEdges();
    for (int i = 0; i < vct2.size(); i++) {
        Hashtable location = (Hashtable) vct2.elementAt(i);
        Enumeration en = location.keys();
        String fv = "";
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = (String)location.get(key);
            fv = fv + value + "-";
        }
        this.add(new LabelField(fv));       
    
    }
    

    可以很容易地通过键从哈希表中获取值:

    (String)location.get("x")  
    (String)location.get("y")  
    (String)location.get("location")  
    (String)edge.get("startnode")  
    (String)edge.get("endnode1")  
    (String)edge.get("distance1")  
    (String)edge.get("endnode2")  
    (String)edge.get("distance2")  
    ...
    
    package filereader;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Hashtable;
    import java.util.Vector;
    
    public class FileReader {
        String locationSection;
        String edgeSection;
        Vector locations;
        Vector edges;
    
        public FileReader(String fileName) {
            // read the contents into the string
            InputStream is = getClass().getResourceAsStream(fileName);
            StringBuffer sb = new StringBuffer();
            int ch;
            try {
                while ((ch = is.read()) != -1) {
                    sb.append((char) ch);
                }
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String text = sb.toString();
    
            // separate locations and edges
            String separator = "_________________________";
    
            // read location section, without last end-of-line char
            int endLocationSection = text.indexOf(separator) - 1;
            locationSection = text.substring(0, endLocationSection);
    
            // read edges section, without end-of-line char after separator
            int startEdgeSection = endLocationSection + separator.length() + 3;
            edgeSection = text.substring(startEdgeSection, text.length());
    
            // parse locations and edges
            locations = getLocationsVector(locationSection);
            edges = getEdgesVector(edgeSection);
        }
    
        // parse locations section
        public Vector getLocationsVector(String section) {
            Vector result = new Vector();
            int startLine = 0;
            int endLine = section.indexOf('\n');
            while (endLine != -1) {
                String line = section.substring(startLine, endLine);
                result.addElement(parseLocationsLine(line, ';'));
                startLine = endLine + 1;
                if (endLine == section.length() - 1)
                    break;
                endLine = section.indexOf('\n', startLine);
                // if no new line found, read to the end of string
                endLine = (-1 == endLine) ? section.length() - 1 : endLine;
            }
            return result;
        }
    
        // parse edges section
        public Vector getEdgesVector(String section) {
            Vector result = new Vector();
            int startLine = 0;
            int endLine = section.indexOf('\n');
            while (endLine != -1) {
                String line = section.substring(startLine, endLine - 1);
                result.addElement(parseEdgesLine(line, ';'));
                startLine = endLine + 1;
                if (endLine == section.length() + 1)
                    break;
                endLine = section.indexOf('\n', startLine);
                // if no new line found, read to the end of string
                endLine = (-1 == endLine) ? section.length() + 1 : endLine;
            }
            return result;
        }
    
        // parse locations line
        public Hashtable parseLocationsLine(String value, char splitBy) {
            Hashtable result = new Hashtable();
            int xCEnd = value.indexOf(splitBy);
            int yCEnd = value.indexOf(splitBy, xCEnd + 1);
            result.put("x", value.substring(0, xCEnd));
            result.put("y", value.substring(xCEnd + 1, yCEnd));
            result.put("location", value.substring(yCEnd + 1, 
                value.length() - 1));
            return result;
        }
    
        // parse edges line
        public Hashtable parseEdgesLine(String value, char splitBy) {
            Hashtable result = new Hashtable();
            int snEnd = value.indexOf(splitBy);
            result.put("startnode", value.substring(0, snEnd));
            int n = 1;
            int start = snEnd + 1;
            int enEnd = value.indexOf(splitBy, snEnd + 1);
            int dstEnd = value.indexOf(splitBy, enEnd + 1);
            while (enEnd != -1 && dstEnd != -1) {
                result.put("endnode" + String.valueOf(n), 
                        value.substring(start, enEnd));
                result.put("distance" + String.valueOf(n), value.substring(
                        enEnd + 1, dstEnd));
                start = dstEnd + 1;
                enEnd = value.indexOf(splitBy, start);
                if (dstEnd == value.length())
                    break;
                dstEnd = value.indexOf(splitBy, enEnd + 1);
                // if last endnode-distance pair, read to the end of line
                dstEnd = (-1 == dstEnd) ? value.length() : dstEnd;
                n++;
            }
            return result;
        }
    
        // getters for locations and edges
        public Vector getLocations() {
            return locations;
        }
    
        public Vector getEdges() {
            return edges;
        }
    
    }
    

    and somewhere in application screen:

    fr = new FileReader("/map.txt");
    Vector vct1 = fr.getLocations();
    for (int i = 0; i < vct1.size(); i++) {
        Hashtable location = (Hashtable) vct1.elementAt(i);
        Enumeration en = location.keys();
        String fv = "";
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = (String)location.get(key);
            fv = fv + value + "-";
        }
        this.add(new LabelField(fv));       
    
    }
    Vector vct2 = fr.getEdges();
    for (int i = 0; i < vct2.size(); i++) {
        Hashtable location = (Hashtable) vct2.elementAt(i);
        Enumeration en = location.keys();
        String fv = "";
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = (String)location.get(key);
            fv = fv + value + "-";
        }
        this.add(new LabelField(fv));       
    
    }
    

    it will be easy to get values from hashtable by keys:

    (String)location.get("x")  
    (String)location.get("y")  
    (String)location.get("location")  
    (String)edge.get("startnode")  
    (String)edge.get("endnode1")  
    (String)edge.get("distance1")  
    (String)edge.get("endnode2")  
    (String)edge.get("distance2")  
    ...
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文