Я пытаюсь прочитать файл строки за строкой с несколькими разделителями. Я использую регулярное выражение для разделения, но не рассматривая пространство ("") как разделители. Файл содержит;, #, ,, и пробел как разделители. Что я делаю не так? файла линия выглядит следующим образом - ADD R1, R2, R3Чтение строки файла с несколькими разделителями в java
public static void initialize() throws IOException {
PC = 4000;
BufferedReader fileReader = new BufferedReader(new FileReader("test/ascii.txt"));
String str;
while((str = fileReader.readLine()) != null){
Instruction instruction = new Instruction();
String[] parts = str.split("[ ,:;#]");
instruction.instrAddr = String.valueOf(PC++);
System.out.println(instruction.instrAddr);
instruction.opcode = parts[0];
System.out.println(instruction.opcode);
instruction.dest = parts[1];
System.out.println(instruction.dest);
instruction.source_1 = parts[2];
System.out.println(instruction.source_1);
instruction.source_2 = parts[3];
System.out.println(instruction.source_2);
}
fileReader.close();}
Выходные печатает 4000 (значение PC), ADD, R1 "" и R2. Как избежать пространства? Что-то не так с regex str.split ("[,:; #]"); ?
Попробуйте использовать «\ s» вместо действительного «» характер. – pathfinderelite
Попробуйте 'str.split (" [,:; #] + ");' со знаком плюса после скобки набора символов? – mgaert
http://stackoverflow.com/questions/5200756/which-is-the-best-way-to-parse-a-file-containing-assembly-language-using-java – IceGlow