1

I am working on test code in which I have to take input from user till user inputs "stop" word and I have to write this to a file. I an getting error in the code.

code :

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();
}

}

I am getting following error :

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

what this error means and how to solve it?

sam
  • 18,509
  • 24
  • 83
  • 116

6 Answers6

4

you can try like

fout.write(word.getBytes());
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
stinepike
  • 54,068
  • 14
  • 92
  • 112
3

Its saying that the method write doesn't take a String parameter.

You need to convert it to a byte array before calling it

How to convert string to byte in Java

Community
  • 1
  • 1
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
3

write function require byte array as first param. So you should convert your string to byte array. You can use word.getBytes("utf-8")

Sergey Kolesov
  • 480
  • 4
  • 16
3

Try

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.
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
1
byte[] dataInBytes = word.getBytes();
fout.write(dataInBytes);

Refer this example.

Sorter
  • 9,704
  • 6
  • 64
  • 74
1

Use FileWriter for character stream, when dealing with characters(String). and also to avoid manual conversion of string to bytes.

 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();

}

}

it will work. If you want to just write logs, use java's logger api.

Rajj
  • 101
  • 7