File I/O Error: 文件名、目录名或卷标语法不正确

huangapple go评论65阅读模式
英文:

File I/O Error: filename, directory name, or volume label syntax is incorrect

问题

I am trying to write a program in Java that accepts a CSV file as user input (either as a file name or file path). The program removes all whitespaces greater than or equal to 3, and replaces it with a comma. The program will then save the modified file to a new location with a user-specified name. I'm currently having difficulties reading files from the user. I get the following error message when I provide an input file:

An error occurred: "C:\...\buzz (The filename, directory name, or volume label syntax is incorrect)"

Here is my main method that runs the program:

package fileConverter;

import java.io.*;
import java.util.*;

public class Program {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter file name or location:");
        String inputFile = scanner.next();

        System.out.println("Enter output file name or location:");
        String outputFile = scanner.next();

        try {
            FileConverter.convertWhitespaceToComma(inputFile, outputFile);
            System.out.println("Conversion complete. Output written to " + outputFile);
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        } finally { 
            scanner.close();
        }
    }
}

This main method calls "FileConverter.java" which is provided below:

package fileConverter;

import java.io.*;
import java.util.regex.Pattern;

public class FileConverter {

    public static void convertWhitespaceToComma(String inputFile, String outputFile) throws IOException {

        // Create a regex pattern for matching whitespace sequences of length 3 or more
        Pattern pattern = Pattern.compile("\\s{3,}");

        FileReader fr = new FileReader(inputFile);
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter(outputFile);
        BufferedWriter bw = new BufferedWriter(fw);

        String line;

        while ((line = br.readLine()) != null) {

            // Replace all matched whitespace sequences with commas
            String modifiedLine = line.replaceAll(pattern.pattern(), ",");
            bw.write(modifiedLine);
            bw.newLine();
        }
        bw.close();
        fw.close();
        br.close();
        fr.close();
    }
}

I have a try-catch block in main which prints an error message (e.getMessage()), and that is where this error message is coming from:

An error occurred: "C:\...\buzz (The filename, directory name, or volume label syntax is incorrect)"

When I compile and run the program this is what comes out of the terminal:

Enter file name or location:
"C:\...\buzz lightyear\...\File Converter\src\fileConverter\<filename.txt>"
Enter output file name or location:
An error occurred: "C:\Users\buzz (The filename, directory name, or volume label syntax is incorrect)
C:\...\buzz lightyear\...\File Converter>

I know there are probably other issues with my code, but let me get this figured out and I will work on tackling the rest!

Any help and/or suggestions are appreciated. I am just trying to identify why the file can't be recognized. Thank you!

As a workaround, I tried providing the file name instead of the full path, but I get this error message instead:

The system cannot find the file specified
英文:

I am trying to write a program in Java that accepts a CSV file as user input (either as a file name or file path). The program removes all whitespaces greater than or equal to 3, and replaces it with a comma. The program will then save the modified file to a new location with a user-specified name. I'm currently having difficulties reading files from the user. I get the following error message when I provide an input file:

An error occurred: &quot;C:\...\buzz (The filename, directory name, or volume label syntax is incorrect)&quot;

Here is my main method that runs the program:

package fileConverter;

import java.io.*;
import java.util.*;

public class Program {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println(&quot;Enter file name or location:&quot;);
        String inputFile = scanner.next();

        System.out.println(&quot;Enter output file name or location:&quot;);
        String outputFile = scanner.next();

        try {
            FileConverter.convertWhitespaceToComma(inputFile, outputFile);
            System.out.println(&quot;Conversion complete. Output written to &quot; + outputFile);
        } catch (IOException e) {
            System.out.println(&quot;An error occurred: &quot; + e.getMessage());
        } finally { 
            scanner.close();
        }
    }
}

This main method calls "FileConverter.java" which is provided below:

package fileConverter;

import java.io.*;
import java.util.regex.Pattern;

public class FileConverter {

    public static void convertWhitespaceToComma(String inputFile, String outputFile) throws IOException {

        // Create a regex pattern for matching whitespace sequences of length 3 or more
        Pattern pattern = Pattern.compile(&quot;\\s{3,}&quot;);

        FileReader fr = new FileReader(inputFile);
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter(outputFile);
        BufferedWriter bw = new BufferedWriter(fw);

        String line;

        while ((line = br.readLine()) != null) {

            // Replace all matched whitespace sequences with commas
            String modifiedLine = line.replaceAll(pattern.pattern(), &quot;,&quot;);
            bw.write(modifiedLine);
            bw.newLine();
        }
        bw.close();
        fw.close();
        br.close();
        fr.close();
    }
}

I have a try-catch block in main which prints an error message (e.getMessage()), and that is where this error message is coming from:

An error occurred: &quot;C:\...\buzz (The filename, directory name, or volume label syntax is incorrect)&quot;

When I compile and run the program this is what comes out of the terminal:

Enter file name or location:
&quot;C:\...\buzz lightyear\...\File Converter\src\fileConverter\&lt;filename.txt&gt;&quot;
Enter output file name or location:
An error occurred: &quot;C:\Users\buzz (The filename, directory name, or volume label syntax is incorrect)
C:\...\buzz lightyear\...\File Converter&gt; 

I know there are probably other issues with my code, but let me get this figured out and I will work on tackling the rest!

Any help and/or suggestions are appreciated. I am just trying to identify why the file can't be recognized. Thank you!

As a workaround, I tried providing the file name instead of the full path, but I get this error message instead:

The system cannot find the file specified

答案1

得分: 2

问题似乎出在使用 scanner.next() 读取到空格为止,所以当你给它路径 C:\Buzz Lightyear 时,它只会读取 C:\Buzz。

改用 nextLine()。

希望能帮助到你。

英文:

Your problem seems to be the use of scanner.next() reads until whitespace, so when you give it the path C:\Buzz Lightyear it will only read C:\Buzz.

Instead use nextLine()

Hope this helps.

huangapple
  • 本文由 发表于 2023年6月6日 07:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定