当我在csv文件中扫描时,我得到了奇怪的输出,用“,”分隔它,对于每行的长度,我将i +“:”附加到第i个元素到printwriter。我的原始输入看起来像什么像这样。
8.035156 7.619141 7.105469
7.234375 7.8125 8.244141
6.615234 8.224609 6.361328
他们确实被“,”分开了。
输出应该看起来像这样
1:8.035156,2:7.619141,3:7.105469,4:7.072266
再次它应该被“,”分开。
但输出看起来像这样,甚至更奇怪,当我点击选择它给我时间。
01:08.0 02:07.6 03:07.1 04:07.1 05:07.4 06:07.2 07:07.6 08:07.1 09:07.1 10:07.2
12:04:06 AM
只有在append语句末尾添加,
字符时才会输出错误的输出。
public class GeneCsv
{
public static void main(String[] args) throws IOException
{
File file = new File("file.csv");
FileWriter writer = new FileWriter("/Users/home/fileExpression.csv");
PrintWriter pw = new PrintWriter(writer);
Scanner in = new Scanner(file);
boolean firstLine = true;
String[] temp = null;
while (in.hasNextLine())
{
if (firstLine == true)
{
pw.println(in.nextLine());
firstLine = false;
continue;
}
else
{
String line = in.nextLine();
temp = line.split(",");
for (int i = 0; i < temp.length; i++)
{
pw.append(i + ":" + temp[i] + ",");
}
pw.append("\n");
}
}
pw.flush();
pw.close();
writer.close();
}
}
答案 0 :(得分:0)
您的代码没有任何问题。问题是Excel将输出解释为一系列时间值。因此,第一列1:8.035156
中的值被读取为1分钟,8.035156秒。如果查看实际数据输出,您会看到它与所需格式匹配。
要让Excel将值视为文本,您需要将它们格式化为
="1:8.035156",="2:7.619141",="3:7.105469",="4:7.072266",
在CSV文件中。领先的=
和周围的引号可以解决问题。
另见Stop Excel from automatically converting certain text values to dates