我应该读取一个文本文件并在每行打印一个带引号的字符串。引用的字符串的大小也必须相同。
这是读取文件:
"check on","SKY yelow, blue ocean","","1598"
"6946","Jaming","Mountain range","GOOO, five, three!","912.3"
这是预期的输出:
check on
SKY yelow, blue ocean
1598
6946
jaming
Mountain range
GOOO, five, three!
912.3
我知道如何阅读文件,但如何获得如上所示的输出?
提前致谢!
答案 0 :(得分:2)
这里是从txt文件中读取数据的代码。这将按照您的意愿打印,我提到下面包含该txt文件的数据
<强>&#34;维鲁&#34;&#34;萨钦&#34;&#34;德拉维德&#34;&#34;古利&#34;&#34;罗希特夏尔&#34; 强>
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class logic {
public static void main(String[] args)
{
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:/Users/rajmohan.ravi/Desktop/test.txt"));
while ((sCurrentLine = br.readLine()) != null) {
reArrange(sCurrentLine.split(","));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void reArrange(String[] dataContent)
{
for(String data : dataContent)
{
System.out.print(data);
System.out.print("\r\n");
}
}
}
答案 1 :(得分:0)
使用Pattern和Matcher类。
List<String> lst = new ArrayList();
Matcher m = Pattern.compile("\"([^\"]+)\"").matcher(string);
while(m.find())
{
lst.add(m.group(1));
}
System.out.println(lst);
答案 2 :(得分:0)
您可以执行以下操作
Scanner scanner = new Scanner(new File("path"));
String input = scanner.next();
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
答案 3 :(得分:0)
您可以从此代码中获取帮助:
String a = "\"abc\",\"xyzqr\",\"pqrst\",\"\""; // Any string (of your specified type)
String an[] = a.split(",");
for (String b : an) {
System.out.println(b.substring(1, b.length() - 1));
}
逐行读取数据并使用上面的代码打印预期结果。