用java编写文件问题

用java编写文件问题,java,file,filewriter,Java,File,Filewriter,我希望结果保存在一个文件中,并使用递归方法重写对象,而不保存任何内容。 如果我尝试全局声明printwriter,那么它会给出错误“默认构造函数无法处理由隐式超级构造函数引发的异常类型UnsupportedEncodingException。必须定义显式构造函数” 这是密码 public void lengthFind() { try { writer = new PrintWriter("Results.txt", "UTF-8"); fileStre

我希望结果保存在一个文件中,并使用递归方法重写对象,而不保存任何内容。 如果我尝试全局声明printwriter,那么它会给出错误“默认构造函数无法处理由隐式超级构造函数引发的异常类型UnsupportedEncodingException。必须定义显式构造函数” 这是密码

public void lengthFind() {
    try {

        writer = new PrintWriter("Results.txt", "UTF-8");
        fileStreamTest obj = new fileStreamTest();
        Scanner s = new Scanner(new File(path));
        int max = 0;

        while (s.hasNextFloat()) {

            int d = (int) s.nextFloat();

            // it filters the time stamp
            if (d <= 5000) {
                String code = obj.GolombCoding(d, dival);

                // finding Max length
                if (code.length() > max) {
                    max = code.length();
                }
            }

        }

        // Dividend Limit check or increase the Dividend
        if (dival == 10) {
            writer.println("Divident has reached it Limit !");
            i++;
            // update the file name
            path = "D:/File Compression/Data/low_freq/low_freq/house_1/channel_"
                    + i + ".dat";
            dival = 10;

        } else {
            dival = dival + 10;
            writer.print("Dividen:" + dival);
        }
        writer.print("Max Length of File " + i + ": " + max);
        writer.println("Path Of the File : " + path);

        int counter = 1;
        // recall the method
        writer.println("Method Call Counter" + counter);

        lengthFind();

    } catch (IOException e) {
        writer.close();
        System.out.println("There is no more File");
        System.exit(0);

    }
public void lengthFind(){
试一试{
writer=新的PrintWriter(“Results.txt”、“UTF-8”);
fileStreamTest obj=新的fileStreamTest();
扫描器s=新扫描器(新文件(路径));
int max=0;
而(s.hasNextFloat()){
int d=(int)s.nextFloat();
//它过滤时间戳
如果(d最大值){
max=code.length();
}
}
}
//股息限额检查或增加股息
if(dival==10){
println(“Divident已达到它的限制!”);
i++;
//更新文件名
path=“D:/File Compression/Data/low\u freq/low\u freq/house\u 1/channel\u”
+i+“.dat”;
dival=10;
}否则{
迪瓦尔=迪瓦尔+10;
作者。打印(“Dividen:+dival”);
}
writer.print(“文件的最大长度”+i+:“+Max”);
println(“文件路径:+Path”);
int计数器=1;
//回想一下这个方法
writer.println(“方法调用计数器”+计数器);
长度查找();
}捕获(IOE异常){
writer.close();
System.out.println(“没有更多文件”);
系统出口(0);
}
查看

抛出:

  • FileNotFoundException—如果给定的文件对象不表示现有的可写常规文件,并且无法创建该名称的新常规文件,或者在打开或创建文件时发生其他错误

  • SecurityException-如果存在安全管理器,并且checkWrite(file.getPath())拒绝对该文件的写入访问

  • UnsupportedEncodingException-如果不支持命名字符集

  • 您需要处理构造函数可能抛出的
    异常。如果要将
    PrintWriter
    声明为
    static
    ,则需要使用静态初始值设定项块

    static PrintWriter pw ;
    static {
       try{
         pw = new PrintWriter("Results.txt", "UTF-8");
         }
         catch(IOException e){
            e.printStackTrace();
            throw new RuntimeException(e);  
        }
    }
    

    究竟什么是模糊的?你看了这张照片了吗?你称之为“全局声明”是什么?全局声明的意思是我在类中创建printwriter对象而不是在methodSo中,在静态初始值设定项中?是的,这给了我UnsupportedEncodingException,所以我找不到将结果写入文件的方法。如果我试图在类中创建对象,它会给出异常,如果我在方法中创建对象,它每次都会覆盖文件,因为该方法是递归的。很抱歉,但这一点也不清楚。从头开始,解释你想要实现什么,你是如何尝试实现的,以及失败的地方