为什么我的 Java 程序在 windows 中运行良好,但在 linux 中却很糟糕?

Why my Java program works perfectly in windows but it's a disaster in linux?

我写了一个读取文本文件的程序,删除请求的字符串并在没有字符串的情况下重写它。该程序从终端获取三个参数:1) 输入文件 2) 字符串 3) 输出文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

class wordfilter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("");
        Scanner conteggio = new Scanner("");
        int numel = 0;
        File file = new File(args[0]); // Argomento 0: il file
        try {
            conteggio = new Scanner(file);
        }
        catch (FileNotFoundException e) {
            System.out.println("File non trovato");
        }

        while (conteggio.hasNext()) {
            numel++;
            conteggio.next();
        }
        conteggio.close();
        String[] lettura = new String[numel];
        int i = 0;
        try {
            scanner = new Scanner(file);
        }
        catch (FileNotFoundException e) {
            System.out.println("File non trovato");
        }
        while (scanner.hasNextLine()) {
            lettura[i] = scanner.next();
            i++;
        }
        System.out.println("Contarighe -> " + numel);
        for (i = 0; i < lettura.length; i++) {
            System.out.println("Elemento " + i + " - > " + lettura[i]);
        }
        scanner.close();
        String escludi = args[1]; // Argomento 1: il filtro
        String[] filtrato = rimuovi(escludi, lettura);
        if (args.length == 3) stampaSuFile(filtrato, args[2]);
    }
    public static String[] rimuovi(String esclusione, String[] input) {
        String[] nuovoV;
        String escludi = esclusione;
        int dim = 0;
        for (int i = 0; i < input.length; i++) {
            if (!input[i].equals(escludi))
                dim++;
        }
        nuovoV = new String[dim];

        int j = 0;
        for (int i = 0; i < input.length; i++) {
            if (!input[i].equals(escludi)) {
                nuovoV[j] = input[i];
                j++;
            }
            ;
        }
        return nuovoV;
    }

    public static void stampaSuFile(String[] out, String path) {
        String closingstring = "";
        File destinazione = new File(path);
        try {
            destinazione.createNewFile();
        } catch (IOException e) {
            System.out.println("Errore creazione file");
        }
        try {   
            FileWriter writer = new FileWriter(destinazione);
            for (int i = 0; i < out.length; i++)
                writer.write(out[i] + (i == (out.length-1) ? closingstring : " "));
            writer.close();
            System.out.println("Scrittura eseguita correttamente");
        } catch (IOException e) {
            System.out.println("Errore scrittura file");
        }
    }
}

在 Windows 没问题,它完美运行。

在 Linux 而不是当我写类似 java wordfilter in.txt word out.txt

的东西时

我明白了

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1478)
    at wordfilter.main(wordfilter.java:42)

有什么问题吗?这是因为 linux?

上的一些差异

输入文件在 Linux 处以换行符结尾。因此,还有另一行,但它是空的。如果您从输入中删除最后一个换行符,程序将开始正常工作。

或者,导入异常

import java.util.NoSuchElementException;

并在代码中忽略它

    while (scanner.hasNextLine()) {
        System.out.println("" + i);
        try {
            lettura[i] = scanner.next();
        } catch (NoSuchElementException e) {}
        i++;
    }

您正在混合基于行和标记的函数,:hasNextLine() 和 next()。如果输入以换行结束(通常在 Linux 上) hasNextLine returns true 在文件末尾,但没有下一个 "item".

    while (scanner.hasNextLine()) {
        lettura[i] = scanner.next();
        i++;
    }

您应该将 hasNext 与 next 一起使用,或者将 hasNextLine 与 nextLine 一起使用,混合使用它们会造成混淆。

    while (scanner.hasNext()) {
        lettura[i] = scanner.next();
        i++;
    }