Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java write to a file

import java.io.FileWriter;   
import java.io.IOException;
...
//write to file
try{
    FileOutputStream writeData = new FileOutputStream("peopledata.ser");
    ObjectOutputStream writeStream = new ObjectOutputStream(writeData);

    writeStream.writeObject(people);
    writeStream.flush();
    writeStream.close();

}catch (IOException e) {
    e.printStackTrace();
}
Comment

java write to file

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
Comment

write in file java

// Java Program to Write Into a File
// using writeString() Method
 
// Importing required classes
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Assigning the content of the file
        String text
            = "Welcome to geekforgeeks
Happy Learning!";
 
        // Defining the file name of the file
        Path fileName = Path.of(
            "/Users/mayanksolanki/Desktop/demo.docx");
 
        // Writing into the file
        Files.writeString(fileName, text);
 
        // Reading the content of the file
        String file_content = Files.readString(fileName);
 
        // Printing the content inside the file
        System.out.println(file_content);
    }
}
Comment

write file java

@Test
public void givenUsingJava7_whenWritingToFile_thenCorrect() 
  throws IOException {
    String str = "Hello";
 
    Path path = Paths.get(fileName);
    byte[] strToBytes = str.getBytes();
 
    Files.write(path, strToBytes);
 
    String read = Files.readAllLines(path).get(0);
    assertEquals(str, read);
}
Comment

PREVIOUS NEXT
Code Example
Java :: queue and stack reader 
Java :: how to create hashmap 
Java :: how to check duplicate string in java 
Java :: how to find armstrong numbers in java 
Java :: Java List Add Elements using add() method 
Java :: check if duplicate element in java 
Java :: how to do annotation configuration in spring 
Java :: java.lang.IllegalStateException: Could not obtain identifier 
Java :: java method parameters 
Java :: text field mouse event java 
Java :: Implementation of LinkedHashMap Class in Java map 
Java :: build libgdx jar 
Java :: get max of array java 
Java :: hdfs java read all files in directory 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: how to convert character into operator 
Java :: java convert double to boolean 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError sdkmanager 
Java :: using java stream filter to find the sum of property 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: reader from string java 
Java :: draw triangle in android studio xml 
Java :: how to code java??????????? 
Java :: android java turn off night mode 
Java :: string to pojo java 
Java :: final method and abstract method 
Java :: java string split underscore 
Java :: Java 8 merge 2 collections. 
Java :: while loops java 
Java :: how to change fragment on button click navigation drawer 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =