找不到合适的写入方法 [英] No suitable method found for write

查看:214
本文介绍了找不到合适的写入方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将先前创建的数组写入文件。
这是我得到的错误,我不知道如何解决它。

Im attempting to write a previously created array to file. This is the error im getting and I don't know how to fix it.

Zoo.java:341: error: no suitable method found for write(Animals)outputWriter.write(animals[p]);
                    ^

method Writer.write(int) is not applicable
  (argument mismatch; Animals cannot be converted to int)

method Writer.write(char[]) is not applicable
  (argument mismatch; Animals cannot be converted to char[])
method Writer.write(String) is not applicable
  (argument mismatch; Animals cannot be converted to String)
method BufferedWriter.write(int) is not applicable
  (argument mismatch; Animals cannot be converted to int)

这是子模块:

public void writeAquatic() throws IOException                                                                
{                                                                                                            
    BufferedWriter outputWriter = null;                                                                      
    String fileName = "Output.txt";                                                                          
    outputWriter = new BufferedWriter(new FileWriter(fileName));                                             
    for(int p = 0; p < animals.length; p++)                                                                  
    {                                                                                                        
        outputWriter.write(animals[p]);                                                                      
        outputWriter.newLine();                                                                              
    }                                                                                                        
    outputWriter.flush();                                                                                    
    outputWriter.close();                                                                                    
}                                                                                                            

背景信息:
数组是一个包含3个对象的数组 - 水生动物,陆生动物和飞行动物 - 所有这些都包含各种功能。
任何帮助都会很棒。

Background Info: The array is an array of 3 objects - Aquatic Animals, Terrestrial Animals and Flying Animals - all which contain various features. Any help would be great.

推荐答案

动物是Java不知道如何将其转换为 String int 的类char

Animals is a class that Java doesn't know how to convert it to String, int, or char.

我假设您要将 Animals 实例数据写入文件 Output.txt

I assume you want to write Animals instance data to file Output.txt.

在这种情况下,你应该定义 toString() Animals 类中的$ c>方法,因此print函数可以将实例转换为字符串并将其写入文件。

In that case, you should define toString() method in Animals class, so the print function can convert the instance to string and write that to the file.

class Animals {
    String name;

    @Override
    public String toString() {
        return "Animal name : " + name;
    }
 }

请参阅:

http://www.javatpoint.com/understanding-toString()-method

这篇关于找不到合适的写入方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆