【问题标题】:no suitable method found for write(String)没有找到适合 write(String) 的方法
【发布时间】:2013-10-30 19:34:50
【问题描述】:

我正在编写测试代码,其中我必须从用户那里获取输入,直到用户输入“停止”字,我必须将其写入文件。我在代码中遇到错误。

代码:

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

public class fh1

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

{

    FileOutputStream fout = new FileOutputStream("a.log");

    boolean status = true;
    while (status)
    {
        System.out.println("Enter the word : ");
        Scanner scan = new Scanner(System.in);
        String word = scan.next();

        System.out.println(word);

        if (word.equals("stop"))
        {
            status = false;
        }
        else
        {
            fout.write(word);
        }
    }
    fout.close();
}

}

我收到以下错误:

fh1.java:28: error: no suitable method found for write(String)
                            fout.write(word);
                                ^
method FileOutputStream.write(byte[],int,int) is not applicable
  (actual and formal argument lists differ in length)
method FileOutputStream.write(byte[]) is not applicable
  (actual argument String cannot be converted to byte[] by method invocation conversion)
method FileOutputStream.write(int) is not applicable
  (actual argument String cannot be converted to int by method invocation conversion) method FileOutputStream.write(int,boolean) is not applicable (actual and formal argument lists differ in length) 1 error

这个错误是什么意思以及如何解决?

【问题讨论】:

    标签: java


    【解决方案1】:

    你可以试试

    fout.write(word.getBytes());
    

    【讨论】:

      【解决方案2】:

      它的意思是方法write不带String参数。

      调用前需要先转换成字节数组

      How to convert string to byte in Java

      【讨论】:

        【解决方案3】:

        write 函数需要字节数组作为第一个参数。所以你应该将你的字符串转换为字节数组。你可以使用 word.getBytes("utf-8")

        【讨论】:

          【解决方案4】:

          试试

          fout.write(word.getBytes());
          

          write(byte[] b):

          public void write(byte[] b)
                     throws IOException
          Writes b.length bytes from the specified byte array to this file output stream.
          Overrides:
          write in class OutputStream
          Parameters:
          b - the data.
          Throws:
          IOException - if an I/O error occurs.
          

          【讨论】:

            【解决方案5】:
            byte[] dataInBytes = word.getBytes();
            fout.write(dataInBytes);
            

            请参阅此example

            【讨论】:

              【解决方案6】:

              在处理字符(字符串)时,将 FileWriter 用于字符流。 并避免手动将字符串转换为字节。

               public class Test14
              
              {
              public static void main(String args[])throws IOException
              
              {
              
              FileWriter fout = new FileWriter("a.log");
              
              boolean status = true;
              while (status)
              {
                  System.out.println("Enter the word : ");
                  Scanner scan = new Scanner(System.in);
                  String word = scan.next();
              
                  System.out.println(word);
              
                  if (word.equals("stop"))
                  {
                      status = false;
                  }
                  else
                  {
                      fout.write(word);
                  }
              }
              fout.close();
              

              }

              }

              它会起作用的。 如果你只想写日志,请使用 java 的 logger api。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-02-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-27
                • 2021-06-19
                • 2015-11-26
                相关资源
                最近更新 更多