Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 如何从txt文件读取更改输入字符串输入格式_Java_String_File_Input_Output - Fatal编程技术网

Java 如何从txt文件读取更改输入字符串输入格式

Java 如何从txt文件读取更改输入字符串输入格式,java,string,file,input,output,Java,String,File,Input,Output,我有以下意见: ["joe","oej","rat","tar","art","atb","tab"] 使用此代码: class Solution { public List<List<String>> groupAnagrams(String[] strs) { if (strs.length == 0)

我有以下意见:

["joe","oej","rat","tar","art","atb","tab"]
使用此代码:

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) return new ArrayList();
        Map<String, List> ans = new HashMap<String, List>();
        for (String s : strs) {
            char[] ca = s.toCharArray();
            Arrays.sort(ca);
            String key = String.valueOf(ca);
            if (!ans.containsKey(key)) ans.put(key, new ArrayList());
            ans.get(key).add(s);
        }
        return new ArrayList(ans.values());
    }
}
然而,输入和输出的格式并不完全符合我的要求

我希望输入文件是一个名为input.txt的txt文件,其中显示为

joe
oej
rat
tar
art
atb
tab
并将输出存储在一个名为output.txt的新txt文件中,如下所示

atb tab
joe oej
art rat tar
我试图读取输入txt文件的操作在我的主文件中:

File file = new File(args[0]);
Scanner scan = new Scanner(file);
要编写输出文件,请执行以下操作:

FileWriter writer = new FileWriter("output.txt");
writer.write(fileContent);
writer.close();

我的问题是:第二个字母段能否简单地实现到
类解决方案中

如果我理解正确,您希望从文件中读取输入,并将输出直接写入解决方案类中的文件。为此,您可以在解决方案中创建一个新方法,并在调用groupAnagrams之前和之后添加该逻辑

class Solution {
    public void fromFile(String inputFilename){
        // Open the file inputFilename here
        // Read the lines into a string array
        List<List<String>> groupAnagrams(strs);
        // Create the output file
        // Write the result in the file
    }
    public List<List<String>> groupAnagrams(String[] strs) {
        //...
    }
}
类解决方案{
public void fromFile(字符串输入文件名){
//在此处打开文件inputFilename
//将这些行读入字符串数组
列出组字图(STR);
//创建输出文件
//将结果写入文件中
}
公共列表组语法(字符串[]strs){
//...
}
}

这样,您就不需要更改方法groupAnagram。

如果我理解正确,您希望从文件中读取输入,并将输出直接写入Solution类中的文件。为此,您可以在解决方案中创建一个新方法,并在调用groupAnagrams之前和之后添加该逻辑

class Solution {
    public void fromFile(String inputFilename){
        // Open the file inputFilename here
        // Read the lines into a string array
        List<List<String>> groupAnagrams(strs);
        // Create the output file
        // Write the result in the file
    }
    public List<List<String>> groupAnagrams(String[] strs) {
        //...
    }
}
类解决方案{
public void fromFile(字符串输入文件名){
//在此处打开文件inputFilename
//将这些行读入字符串数组
列出组字图(STR);
//创建输出文件
//将结果写入文件中
}
公共列表组语法(字符串[]strs){
//...
}
}
这样,您就不需要更改方法groupAnagram