仅删除某些地方的空格,而不是所有地方
我正在从 txt 文件中获取 txt 并尝试将其存储在数组列表中。在txt文件中,这意味着选票不在一起,而是老师将其放在单独的行上。因此,我们必须将所有选票放在一起,但我无法将它们放在一起,她将第一张选票放在了线上,如下例所示。我们必须把剩下的放在一起。我正在使用 fileinputstream 从文本文件中收集 txt。
文本如下所示:
<前><代码>人 1 人 2 人 3 1 2 3 1 3 2
我希望它看起来像这样
<前><代码>人 1 人 2 人 3 1 2 3 1 3 2
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name: ");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
File file = new File(fileName);
ArrayList<String> ballot;
ballot = new ArrayList<String>();
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream ds = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(ds));
// Pattern p;
// Matcher m;
String strLine;
String inputText = "";
String newline = System.getProperty("line.separator");
while ((strLine = br.readLine()) != null) {
ballot.add(strLine);
}
I am taking in txt from a txt file and trying to store it in an arraylist. In the txt file the which mean ballot are not together instead the teacher placed it on a separate line. So we have to get all of the ballots together but i am not able to get them to be all together she placed the first ballot on the line like in example below. And we have to make the rest of them together. I am using a fileinputstream to collect the txt from the textfile.
the text looks like this :
person 1 person 2 person 3 <b> 1 2 3 <b> 1 3 2
I want it to look like this
person 1 person 2 person 3 <b> 1 2 3 <b> 1 3 2
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name: ");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
File file = new File(fileName);
ArrayList<String> ballot;
ballot = new ArrayList<String>();
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream ds = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(ds));
// Pattern p;
// Matcher m;
String strLine;
String inputText = "";
String newline = System.getProperty("line.separator");
while ((strLine = br.readLine()) != null) {
ballot.add(strLine);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
StringBuilder sb = new StringBuilder();
。每次您拥有令牌String t
时,请调用sb.append(t.trim()).append(' ');
。解析完文件后,调用 sb.toString();。如果您想在各处添加换行符,请使用sb.append('\n');
。如果您想要一个令牌数组,您应该使用 ArrayList; al = new ArrayList; 每次有一组 token 组成一行时,使用
al.add(s);
完成后,调用String[] result = al.toArray(new String[al.length]);
您需要将每行的标记集连接到 s 中。You need to use a
StringBuilder sb = new StringBuilder();
. Each time you have a tokenString t
, callsb.append(t.trim()).append(' ');
. When done with parsing your file, callsb.toString();
. If you want to add newlines here and there, usesb.append('\n');
.If you want an array of your tokens, you should use an
ArrayList<String> al = new ArrayList<String>;
Each time you have a set of token s making a line, useal.add(s);
When done, callString[] result = al.toArray(new String[al.length]);
You will need to concatenate your set of tokens into s for each line.