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 content to file java

public void whenWriteStringUsingBufferedWritter_thenCorrect() 
  throws IOException {
    String str = "Hello";
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    writer.write(str);
    
    writer.close();
}
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 :: @crossorigin spring allow all 
Java :: how to get 2 decimal places in java 
Java :: print string in java 
Java :: jtable font size 
Java :: cast java 
Java :: java program to find the power of a number 
Java :: while loop in java 
Java :: how to count the number of occurrences of an element in a arraylist in java 
Java :: java initialize class 
Java :: java first character string number 
Java :: java get object from string name 
Java :: how to make arraylist character 
Java :: java split int 
Java :: java stream and filter 
Java :: creating a menu in java 
Java :: checkc if string is double java 
Java :: java mcq 
Java :: multiple condition inside for loop java 
Java :: java actionevent 
Java :: hashmap sort ascending 
Java :: java check if property exists in class 
Java :: list of string to string array in java 
Java :: how to name loops in java 
Java :: print symbol in pyramid shape in java 
Java :: java pointer 
Java :: method java 
Java :: java access a file 
Java :: java create arraylist with size 
Java :: maximum arrays size in java 
Java :: Fix arabic javafx 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =