Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将每个值添加到单独的行中?_Java_File_Try Catch_Read Write - Fatal编程技术网

Java 如何将每个值添加到单独的行中?

Java 如何将每个值添加到单独的行中?,java,file,try-catch,read-write,Java,File,Try Catch,Read Write,我的代码确实读取和写入文件,但它不是针对每个值都在一行中,而是在一行中打印每个值 // 2 points static void Q1(String inputFilename, String outputFilename) { // You are given a csv file (inputFilename) with all the data on a single line. Separate the // values by commas and write each

我的代码确实读取和写入文件,但它不是针对每个值都在一行中,而是在一行中打印每个值

// 2 points
static void Q1(String inputFilename, String outputFilename) {
    // You are given a csv file (inputFilename) with all the data on a single line. Separate the
    // values by commas and write each value on a separate line in a new file (outputFilename)

        String data = "";
        try {
            for(String s :Files.readAllLines(Paths.get(inputFilename))){
                data = data + s;
            }
            Files.write(Paths.get(outputFilename), data.getBytes());
        } catch (IOException e) {

            e.printStackTrace();
        }
}
因此,评分员说:

Incorrect on input: [data/oneLine0.csv, output0.txt]
Expected output : overwrought plastic bomb
wrapped litter basket
obstetric matter of law
diabetic stretching
spatial marathi
continental prescott
reproductive john henry o'hara
hollow beta blocker
stereotyped national aeronautics and space administration
irremediable st. olaf
brunet fibrosis
embarrassed dwarf elm
superficial harrier
disparaging whetstone
consecrate agony
impacted lampoon
nefarious textile
some other organisation
Your output     : overwrought plastic bomb,wrapped litter basket,obstetric matter of law,diabetic stretching,spatial marathi,continental prescott,reproductive john henry o'hara,hollow beta blocker,stereotyped national aeronautics and space administration,irremediable st. olaf,brunet fibrosis,embarrassed dwarf elm,superficial harrier,disparaging whetstone,consecrate agony,impacted lampoon,nefarious textile,some other organisation

首先,您需要从CSV文件中删除逗号。我建议使用
s=s.replace(“,”,”)
此外,您必须向每个字符串追加一个
\n
,使其显示在新行上。因此,您应该添加
s+=“\n”这将生成以下代码:

// 2 points
static void Q1(String inputFilename, String outputFilename) {
// You are given a csv file (inputFilename) with all the data on a single line. Separate the
// values by commas and write each value on a separate line in a new file (outputFilename)

    String data = "";
    try {
        for(String s :Files.readAllLines(Paths.get(inputFilename))){
            s.replace(",","");
            s += "\n";
            data = data + s;
        }
        Files.write(Paths.get(outputFilename), data.getBytes());
    } catch (IOException e) {

        e.printStackTrace();
    }
}

首先,您需要从CSV文件中删除逗号。我建议使用
s=s.replace(“,”,”)
此外,您必须向每个字符串追加一个
\n
,使其显示在新行上。因此,您应该添加
s+=“\n”这将生成以下代码:

// 2 points
static void Q1(String inputFilename, String outputFilename) {
// You are given a csv file (inputFilename) with all the data on a single line. Separate the
// values by commas and write each value on a separate line in a new file (outputFilename)

    String data = "";
    try {
        for(String s :Files.readAllLines(Paths.get(inputFilename))){
            s.replace(",","");
            s += "\n";
            data = data + s;
        }
        Files.write(Paths.get(outputFilename), data.getBytes());
    } catch (IOException e) {

        e.printStackTrace();
    }
}