Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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_Map_Character_Fileinputstream - Fatal编程技术网

Java 如何访问参数的参数?

Java 如何访问参数的参数?,java,map,character,fileinputstream,Java,Map,Character,Fileinputstream,这是一项正在进行的工作,但我想知道如何访问特定的文件。 我的主要方法是用一个新文件作为参数来构造一个新的FileInputStream。然后,我应该调用接受FileInputStream对象的getCounts方法来返回某种映射。在getCounts方法中,我很难完成这项工作,因为我必须能够访问其中的文件,而FileInputStream似乎没有访问器。换句话说,我如何访问这个用于构造FileInputStream对象的文件,该对象作为参数进入getCounts方法,位于getCounts方法内

这是一项正在进行的工作,但我想知道如何访问特定的文件。 我的主要方法是用一个新文件作为参数来构造一个新的FileInputStream。然后,我应该调用接受FileInputStream对象的getCounts方法来返回某种映射。在getCounts方法中,我很难完成这项工作,因为我必须能够访问其中的文件,而FileInputStream似乎没有访问器。换句话说,我如何访问这个用于构造FileInputStream对象的文件,该对象作为参数进入getCounts方法,位于getCounts方法内部?最终,我应该使用映射的键/值来获取文本文件中最频繁出现的字符。谢谢

这是我的密码:

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


public class test2 {
public static void main(String[] q) throws FileNotFoundException {

    // This is for the character mapping
    Scanner console = new Scanner(System.in);
    System.out.println("Count characters of which file? ");
    FileInputStream output = new FileInputStream(new File(console.nextLine()));
    Map<Character, Integer> results = getCounts(output); 
    // do stuff with this map later on...
}

 // character counting method (WIP)
 public static Map<Character, Integer> getCounts(FileInputStream input) {
     Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
     // Problem here: need to access the file object that was instiantiated
     byte[] fileContent = new byte[(int) file.length()]; // puts all the bytes from file into byte[] to process
     input.read(fileContent); 
     String test = new String(fileContent);

     // missing stuff here; use map to map keys as characters and occurrences as values.

     return output;
 }
}
import java.util.*;
导入java.io.*;
公共类test2{
公共静态void main(字符串[]q)引发FileNotFoundException{
//这是用于角色映射的
扫描仪控制台=新扫描仪(System.in);
System.out.println(“计算哪个文件的字符数?”);
FileInputStream输出=新的FileInputStream(新文件(console.nextLine());
映射结果=getCounts(输出);
//以后再处理这张地图。。。
}
//字符计数法(WIP)
公共静态映射getCounts(FileInputStream输入){
Map output=new TreeMap();//TreeMap按排序顺序保存键(按字母顺序排列的字符)
//这里的问题:需要访问已建立的文件对象
byte[]fileContent=new byte[(int)file.length()];//将文件中的所有字节放入byte[]以进行处理
输入.读取(文件内容);
字符串测试=新字符串(fileContent);
//此处缺少内容;使用map将键映射为字符,将引用映射为值。
返回输出;
}
}

如果要使用FileInputStream,则需要循环执行多次读取

byte[] fileContent = new byte [1024];

while ((input.read (fileContent) != -1) {

     // save fileContent somewhere
     // e.g.
     arrlist.add (new String (fileContent));

}

理想情况下,我会将
length
作为参数传递给
getCounts()
,但由于不允许这样做,您可以将文件长度保存到类静态参数中:

private static long length;

public static void main(String[] q) throws IOException {

        // This is for the character mapping
        Scanner console = new Scanner(System.in);
        System.out.println("Count characters of which file? ");
        File file = new File(console.nextLine());
        FileInputStream output = new FileInputStream(file);
        length = file.length();
        Map<Character, Integer> results = getCounts(output);
        // do stuff with this map later on...
    }

    // character counting method (WIP)
    public static Map<Character, Integer> getCounts(FileInputStream input) throws IOException {
        Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
        // Problem here: need to access the file object that was instantiated
        byte[] fileContent = new byte[(int) length]; // puts all the bytes from file into byte[] to process
        input.read(fileContent);
        String test = new String(fileContent);
        System.out.println("test = " + test);

        // missing stuff here; use map to map keys as characters and occurrences as values.

        return output;
    }
私有静态长长度;
公共静态void main(字符串[]q)引发IOException{
//这是用于角色映射的
扫描仪控制台=新扫描仪(System.in);
System.out.println(“计算哪个文件的字符数?”);
File File=新文件(console.nextLine());
FileInputStream输出=新的FileInputStream(文件);
length=file.length();
映射结果=getCounts(输出);
//以后再处理这张地图。。。
}
//字符计数法(WIP)
公共静态映射getCounts(FileInputStream输入)引发IOException{
Map output=new TreeMap();//TreeMap按排序顺序保存键(按字母顺序排列的字符)
//这里的问题是:需要访问实例化的文件对象
byte[]fileContent=newbyte[(int)length];//将文件中的所有字节放入byte[]以进行处理
输入.读取(文件内容);
字符串测试=新字符串(fileContent);
System.out.println(“test=“+test”);
//此处缺少内容;使用map将键映射为字符,将引用映射为值。
返回输出;
}

为什么需要访问文件对象?输入流确实为您提供了足够的数据进行计数。出于某种原因,分配要求getCounts接受FileInputStream并返回映射。我相信他只是想要
文件
对象,以便他可以进行
文件。长度
确定谢谢。因为我只允许将FileInputStream作为参数,所以我必须手动定义数组大小,就像可怕的袋熊建议的那样。@ScaryWombat 3个原因:1)它工作正常。2) 这是一个很好的解决办法。3) 最小的代码更改。因为他正在阅读整个大型小说,我不确定在一块中读取那么多数据是否是一个好的解决方案,甚至可能这听起来像一个愚蠢的问题,但为什么字节[]文件内容有1024个元素?这是一个任意数,可能是2048、1234或6734,但是inputstream在返回之前只会读取那个数量的数据。我计划使用它来读取大型小说中的角色,所以我应该有如下内容:byte[]filecontent=new byte[Integer.MAX_VALUE];只是为了安全?看到了吗