- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 TAB 分隔输入文件有 100 万行,它看起来像这样:
id name artist_name genre notoriete_fr notoriete_us notoriete_uk notoriete_it notoriete_sp notoriete_no notoriete_de notoriete_wd
1 10ème bougie 113 rap 0 -5 -5 -5 -5 -5 -5 -5
2 I'm not in love 10cc pop 1 1 1 1 1 1 1 1
5 Generation Black Rebel Motorcycle Club rock 0 0 0 0 0 0 0 0
我编写了文件格式转换代码,输出文件如下所示:
id:ID;genre;notoriete_fr:int;notoriete_us:int;notoriete_uk:int;notoriete_sp:int;notoriete_de:int;notoriete_it:int;notoriete_no:int;notoriete_wd:int;:LABEL
t1;rap;0;-5;-5;-5;-5;-5;-5;-5;Track
t5;rock;0;0;0;0;0;0;0;0;Track
我有两个问题:
t2
的行丢失了这是我的代码,提前致谢!
注意:我还在 new BufferedWriter()/Reader()
中添加了缓冲区大小,没有影响。
public static void main(String[] args) throws Exception {
BufferedReader br = null;
BufferedWriter bw = null;
try{
// prepare input file
File inFile = new File(inputFile);
br = new BufferedReader(new FileReader(inFile));
String line = "";
String cvsSplitBy = "\t";
// prepare output file
File outFile = new File(outputFile);
bw = new BufferedWriter(new FileWriter(outFile));
// Write header
bw.write("id:ID;genre;notoriete_fr:int;notoriete_us:int;notoriete_uk:int;notoriete_sp:int;notoriete_de:int;notoriete_it:int;notoriete_no:int;notoriete_wd:int;:LABEL\n");
while ((line = br.readLine()) != null) {
// READING
line = br.readLine();
String[] features = line.split(cvsSplitBy);
// WRITING
bw.write("t"+features[0]+";"+features[3]+";"+features[4]+";"+features[5]+";"+features[6]+";"+features[7]+";"+features[8]+";"+features[9]+";"+features[10]+";"+features[11]+";Track\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
最佳答案
the output file only has 8.3% of input file lines
就您的代码而言,应该缺失 50% 的行。您在大小上存在差异,因为父文件中的数据与您正在创建的文件中的数据格式不同。我这样说是因为您的代码跳过了备用行。
让我解释一下,在你的 while 循环条件下,你正在使用 line = br.readLine()
读取第 1 行。现在在 while 循环的第一行你再次使用 line = br.readLine()
这将读取文件的第 2 行。您正在使用它来写入数据,因此第 2 行数据被写入。现在在第二个循环中,在 while 循环条件下,您正在读取文件的第 3 行,而在 while 循环的第一行中,您正在读取文件的第 4 行,并且这一行被写入。所以你看到你得到了 50% 的输出。
现在您认为您理解了为什么输出文件中的行变少了。所以简单的解决方案是去掉 while 循环的第一行,让条件保持不变。
关于Java Buffered FileWriter 仅写入 50% 的输入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304330/
我有一段代码,只要有可用的新数据作为 InputStream 就会生成新数据。每次都覆盖同一个文件。有时文件在写入之前变为 0 kb。 Web 服务会定期读取这些文件。我需要避免文件为 0 字节的情况
请帮我解决这个问题。我正在尝试编写一个读取 .txt 文件的代码,然后它会计算文件中每个字母的频率。这就是我想到的: public static void charCount(String file)
我一直找不到原因。我在这段代码中遇到的唯一问题是,当 FileWriter 尝试将新值放入文本文件时,它会放入一个 ?。我不知道为什么,甚至不知道这意味着什么。这是代码: if (secMessage
我正在尝试编写一个Java程序,其中用户指定他们想要输入多少“学生记录”,然后是学生的姓名、年龄和GPA,然后将其存储为文本。然而,我的文本有问题,不包括所有输入的数据和一个我无法摆脱的神秘的悬空换行
我正在尝试使用 XLS2CSV 将 Excel 文件转换为 csv 文本文件。我稍微修改了现有的类,将 PrintStream 更改为 Writer,这样我就可以使用 FileWriter 写入文件。
我使用文件编写器以类似 CSV 的格式将对象数组列表作为字符串写入文件。不管怎样,我在让它工作时遇到了问题,但现在我收到一个 FileNotFound 异常,它说创建的文件在异常中是只读的。正如我检查
我有一个程序,允许用户将信息写入文件。它不会覆盖原始文件中的所有内容,而是添加到其中。问题是,如果用户没有输入足够的信息,程序就会崩溃。 文本文件中的每一行看起来都像这样: info 1, Info
我正在热点虚拟机中的 Windows 2008 服务器(64 位)上运行我的 java 应用程序。 几个月前,我创建了一个工具来帮助检测应用程序中的死锁。在过去一个月左右的时间里,唯一给我带来任何问题
try { File file = new File(filepath+"temp.txt"); if (!file.exists()) { f
所以,基本上它可以工作,但事实并非如此。它不会抛出任何错误,只是没有完成写入文件。它确实读取了文件中的所有行,并且它们的格式都正确。我已经尝试过调试所有这些。调试“currentLine”时,所有行都
我正在尝试编写一个快速的java程序来获取csv并在SQL中创建插入语句。我不需要所有列,否则我将直接通过 csv 导入数据。当我实际写入文件时,我的特殊问题就出现了。编写器应该向文件输出 200 多
我不确定为什么我的输出函数在创建文件时没有拾取换行符。这是代码: private void outputToFile(){ final JFileChooser fc = new JFileC
我对 Java 还很陌生。我试图将“List:”添加到新文本文件的开头(如果它不存在)。相反,文本文件是空白的,输入内容位于一行空白下方。 File hi = new File("hi.txt");
好吧...所以我有一个相当有趣的错误...我声明了一个名为 file 的 FileWriter,并且让它经历以下 for 循环: for (int i = 0; i < a.radtot; i++)
我正在开发一个 Java 应用程序,它有一个 JTextArea 供用户输入文本。它可以是任意数量的行,但是我的 FileWriter 遇到了问题,它只保存了任何输入的第一行。我以前从未使用过 Swi
我试图在 *.txt 文件中添加整数值,但它打印的是 ASCII 值。 我的代码有什么问题? public static void main(String[] args) throws IOExcep
我正在使用几个 java FileWriter 将数据附加到输出文件。这些文件在处理过程中全部保持打开状态,并定期写入(附加)。有时,我在逻辑中遇到需要删除当前打开文件的内容(文件长度变为零)的情况,
我有一个简单的程序,可以读取命令并执行它们。现在我有这段代码用于将某些文本插入到文本文件中: 示例命令: INSERT "John Smith" INTO college.student 我的主要方法
我很困惑... public class Testing { public static void main(String[] args) throws FileNotFoundExcepti
在下面的 FileWriter 中,我正在写入一个名为“employee.txt”的文件。它写入文件,但最初它只是附加到文件而不是每次都进入新行。从那时起,我通过添加“\n”来编辑代码,使其下降一行。
我是一名优秀的程序员,十分优秀!