Java 在不同的数组中逐列读取文本文件
我有这个文本文件Java 在不同的数组中逐列读取文本文件,java,arrays,string,Java,Arrays,String,我有这个文本文件 SFrm EFrm SegAScr Phone 0 36 -158051 SIL 37 105 -644247 +NONTRANS+ 106 109 -96452 l SIL w b 110 112 -125055 w l aa i 113 115 -150550 aa w 7 i 116 118 -146662 7 aa i i 119 122 -46757 i 7 d i
SFrm EFrm SegAScr Phone
0 36 -158051 SIL
37 105 -644247 +NONTRANS+
106 109 -96452 l SIL w b
110 112 -125055 w l aa i
113 115 -150550 aa w 7 i
116 118 -146662 7 aa i i
119 122 -46757 i 7 d i
123 126 -58440 d i SIL e
127 146 -90776 +MUSIC+
147 152 -61098 t SIL u b
153 158 -67393 u t f i
159 174 -251284 f u f i
175 178 -79772 f f aa i
179 194 -134562 aa f 7 i
195 206 -33695 7 aa a i
207 223 -194024 a 7 SIL e
224 350 -434997 +NOISE+
351 353 -28280 SIL
Total score: -2802095
我设法将整个内容存储在一个字符串中,但我需要将其存储在一些数组中,其中每个列都表示在不同的数组中,我知道可以使用.split()作为将其转换为数组的过程,但我无法丢弃列之间的空格
ps:文本文件是通用文件,因此这些数字和字母不是常量,但其形式是常量(4列)
我现在的主要问题是在第四列中任何一行的开头捕捉重复的元音,并对同一行的数字进行一些计算,如果有人比我更容易,请提供任何帮助:)我希望这有助于:
public static void main(String[] args) throws FileNotFoundException, IOException {
FileReader inputFile = new FileReader("input");
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line="";int index=0;
String[] column1= new String[100];
String[] column2 = new String[100];
String[] column3=new String[100];
String[] column4=new String[100];
while ((line = bufferReader.readLine()) != null){
String temp="";int count=1;
column4[index]="";
//System.out.println(line);
StringTokenizer st = new StringTokenizer(line," ");
//String tokenizer gets the token from each space
while(st.hasMoreTokens())
{
temp = st.nextToken();
//System.out.println(temp);
If(temp.equals("Total")){
break;
}
if(count==1)
{
// System.out.println(temp);
column1[index] = temp;
}
if(count==2){
column2[index] = temp;
}
if(count==3)
{
column3[index] = temp;
}
if(count==4)
{
column4[index] += temp;
}
if(count<4)
count++;
}
index++;
}
for(int i=0;i<index-1;i++){
System.out.println(column1[i]+" "+column2[i]+" "+column3[i]+" "+column4[i]);
}
}
您需要逐行读取文件,拆分行并将值存储在各自的数组中。文件数据中的空格非常凌乱,是否有任何方法可以更改数据的格式。否则,您将很难拆分。这些列的宽度是否固定?以更面向对象的方式,您不会将每行拆分为4个数组中的一个条目;您将读取一行并从该行创建一个对象,并保留单个对象数组(或
列表
)。也许用a来区分每一行。@denis我无法更改此文件是我正在处理的引擎的输出,它是sphinx3_alignerI我真的很感激你的辛勤工作,谢谢你,但我没有澄清我的4列错误!你可以修改第四栏的代码。我真的很感激你的辛勤工作。谢谢你,但我没有澄清我的4栏我的不好!我的4列是(SFrm->351),第二列是(Efrm->353),第三列是(SegAScr->-28280),最后第四列是(Phone->SIL),它将丢弃在数组中只有它的第一个字的总分行。我真的很抱歉我不好的澄清,再次感谢你的帮助,我会尝试修改你的,以实现我需要的。谢谢所有这四列都在我代码的列数组中。第一列在第一个数组中,第二列在第二个数组中,依此类推。什么不符合您的要求?
SFrm EFrm SegAScr Phone
0 36 -158051 SIL
37 105 -644247 +NONTRANS+
106 109 -96452 lSILwb
110 112 -125055 wlaai
113 115 -150550 aaw7i
116 118 -146662 7aaii
119 122 -46757 i7di
123 126 -58440 diSILe
127 146 -90776 +MUSIC+
147 152 -61098 tSILub
153 158 -67393 utfi
159 174 -251284 fufi
175 178 -79772 ffaai
179 194 -134562 aaf7i
195 206 -33695 7aaai
207 223 -194024 a7SILe
224 350 -434997 +NOISE+
351 353 -28280 SIL