Save String to a file by converting the text to a byte array - Java File Path IO

Java examples for File Path IO:File Operation

Description

Save String to a file by converting the text to a byte array

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path rf_wiki_path = Paths.get("C:/folder1/wiki", "wiki.txt");
    String rf_wiki = "this is a test";

    try {/*w w w. j  a v a 2 s .co m*/
      byte[] rf_wiki_byte = rf_wiki.getBytes("UTF-8");
      Files.write(rf_wiki_path, rf_wiki_byte);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials