Answers for "file to fileinputstream java"

0

write files with FileOutPutStream java

@Test
public void givenWritingStringToFile_whenUsingFileOutputStream_thenCorrect() 
  throws IOException {
    String str = "Hello";
    FileOutputStream outputStream = new FileOutputStream(fileName);
    byte[] strToBytes = str.getBytes();
    outputStream.write(strToBytes);

    outputStream.close();
}
Posted by: Guest on November-28-2021
0

Java FileOutputStream to write data to a File

import java.io.FileOutputStream;
public class Main {
    public static void main(String[] args) {
       String data = "This is a line of text inside the file.";
        try {
            FileOutputStream output = new FileOutputStream("output.txt");

            byte[] array = data.getBytes();

            // Writes byte to the file
            output.write(array);

            output.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}
Posted by: Guest on May-25-2022

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language