/ / Analisando um arquivo de texto delimitado por um pipeline [fechado] - android

Analisando um arquivo de texto delimitado por um pipeline [fechado] - android

Sou um novato no desenvolvimento de Android, só preciso saber se há alguma maneira de analisar um arquivo de texto, delimitado por um pipeline?

Obrigado,

Respostas:

0 para resposta № 1

Eu acho que você quer ler o conteúdo de um arquivo de texto e quer todos os dados em pedaços de String separados com base no pipeline? SE eu estiver certo, seguir o código seria útil:

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 ;
}


}

Agora chame o código acima da seguinte forma:

FileReader fileReader = new FileReader("Path to your text file");
ArrayList<String> stringsSeperatedByPipeDelimeters = fileReader.read();

0 para resposta № 2

Simplesmente leia seu arquivo em uma String e divida a String usando pipeline ou qualquer outro delimitador.


-1 para resposta № 3

Você também pode fazer algo assim:

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;
}