0

我是android开发的新手,只需要知道是否有任何方法可以解析由管道分隔的文本文件?

谢谢,

4

3 回答 3

0

只需将文件读入字符串并使用管道或任何其他分隔符拆分字符串。

于 2013-02-07T07:40:36.490 回答
0

我认为您想读取文本文件的内容并希望基于管道将字符串中的整个数据分开?如果我是对的,下面的代码会有所帮助:

public class FileReader {
      private String fileType;
      FileReader (String targetFilePath){
        File file = new File(targetFilePath);
        if (!file.exists()) {
            Log.e("",targetFilePath + " does not exist.");
        } else if (!(file.isFile() && file.canRead())) {
            Log.e("",file.getName() + " cannot be read from.");
        } else {
            try {
                fileInputStream = new FileInputStream(file);
                fileBufferedInputStream = new BufferedInputStream(fileInputStream);
                //Use FileBufferedInputStream just for supporting mark()/reset() while reading 
                // reading file extension ex. java or xml or txt
                this.fileType = targetFilePath.substring(
                        targetFilePath.lastIndexOf('.') + 1,
                        targetFilePath.length());

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Log.e("",""+e.printStackTrace());
            }
        }
      }


/*
This method will return a arraylist of Strings which are seperated by '|' in given  txt file 
and null otherwise like if file was not text,blank.etc
*/
    public ArrayList<String> read(){ 
        if(fileType.equalsIgnoreCase("txt")){
        char currentChar;
        ArrayList<String> myStringsList ;
        while (fileBufferedInputStream.available() > 0) {
               StringBuilder stringBuilder = new StringBuilder();
               while ((currentChar = (char) fileBufferedInputStream.read()) == '|') {
                   stringBuilder.append(current);
               }
               if(null == myStringsList){
                 myStringsList = new ArrayList<String>();
               }  
               myStrings.add(stringBuilder.toString());
        }    
        }
        fileBufferedInputStream.close();
        return myStringsList ;
}


 }

现在调用上面的代码如下:

FileReader fileReader = new FileReader("Path to your text file");
ArrayList<String> stringsSeperatedByPipeDelimeters = fileReader.read();
于 2013-02-07T08:02:11.957 回答
-1

你也可以这样做:

private String[] getPipedChunks(String targetFilePath){
           File file = new File(targetFilePath);
           try {
                    fileInputStream = new FileInputStream(file);
           }
           } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  Log.e("",""+e.printStackTrace());
           }
           StringBuilder sb;
           while(fileInputStream.available() > 0) {

       if(null== sb)  sb = new StringBuilder();

            sb.append((char)fileInputStream.read());
       }
       String stringArray [];
       if(null!=sb){
            stringArray = sb.toString.split('|');
            // This is your String Array.


       }
       try {
          fileInputStream.close();
       }
       catch(Exception e){
           // TODO Auto-generated catch block
           Log.e("",""+e.printStackTrace());
       }
           return stringArray;
}
于 2013-02-07T08:12:45.980 回答