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 :: codepointat java 
Java :: scala vs java 
Java :: java polymorphism 
Java :: char array to string in java 
Java :: lauch java batch 
Java :: sorting collections with comparator java 
Java :: reading string after double in java 
Java :: initialize arraylist java with size 
Java :: java ints to color int 
Java :: Java How to use Map? 
Java :: input using stringbuffer 
Java :: Java How to use List? 
Java :: select list of values from db java 
Java :: how to find the length of an array in java 
Java :: fragment view in XML android 
Java :: frame background changing in java 
Java :: Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main 
Java :: android arraylist to comma separated string 
Java :: How to find the maximum occurring character in a given String? using hashmap java 
Java :: pdf intent does not have permission to launch 
Java :: jsp check if request parameter exists 
Java :: horizontal recyclerview item width half of screen android 
Java :: enhance for loop java 
Java :: how to copy an object in java 
Java :: treemap java entryset 
Java :: java list newline 
Java :: Compare two csv files using java 
Java :: android studio json parser 
Java :: how to call a static method in java 
Java :: Simple For Loop Java Example 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =