本文介绍了在Java中读取int形式的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个* .txt文件,其中包含这样的数据:
I have a *.txt file that it has data like this:
1222 25 36 25 14 25 25 36 363 25 15
1253 69 54 87 54 285
]±غ'Qہx¸'،2ذç12â· 'ئ‰؟¦خ&{3ع*U6هؤر–¨ر،³ڑُں¢œغ)™پ÷ةtڑت†éYْ(زH5x¸2ش/¨#ژظœ,tx[Kh6"¨
rٹ±k'¨اqaيïذüـvqشQ0H888/ حlںR–>Kْ¹bف‘دô†)oŒىٹط.fNؤ8ک„ٌnpwَ§IMقJ™؟س5؛x.Zµ™7ˆے¨أئ°—لف):©¢چR¢سï¶J±@JœOْ5TMè§è9´«7 –دس54)ںشw>’âغ2›Zi@وûr& طFو-dة ôƒ( œءxƒ§أh(¢ش‘»إV¨پ~ؤF؟!]&´ye\جہ„°?ّ!Uج3صwyc†P`¬:
ِS…ةّEژœ Zشâku X§Rٌ¦ص«{â‹YwOڈ48¹Wٌ"i¾َه#™²|(³bˆiتژ-»çJ¯صl¦ر"+ءC’µہڈ™،£ظ(2€j¤ًگdك(`اء—꯳[f
其中的前17个字符为整数,其余的为二进制.
first 17 chars of that are integer and others are binary.
现在我想先阅读17个字符.我该怎么读?
now i want to read first 17 chars.how can i read them?
推荐答案
您可以使用 java.io.Scanner
:
This is something you can do with a java.io.Scanner
:
File f = new File("yourtxt.txt");
Scanner s = new Scanner(f);
for (int i = 0; i < 17 && s.hasNextInt(); ++i)
{
int inputInteger = s.nextInt();
// Handle your int here...
}
引发的异常可能是因为整数之间不需要字节.
The exception that is thrown is probably because of bytes you don't need between the integers.
也许您可以尝试执行以下操作:
Maybe you can try to do something like this:
DataInputStream dis = new DataInputStream(new FileInputStream(yourFile));
String numbers = dis.readLine() + " " + dis.readLine();
numbers = numbers.trim().replaceAll(" +", " ");
String[] array = numbers.split(" ");
for (int i = 0; i < array.length; ++i)
{
int inputInteger = Integer.parseInt(array[i]);
// handle inputInteger here...
}
这篇关于在Java中读取int形式的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!