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 :: Java printf() Method using PrintWriter 
Java :: add element in the last in double linked list in java 
Java :: android ecode base64 
Java :: @override java example 
Java :: check first character of string is number java 
Java :: java read integer from text file into array scanner 
Java :: java array sortieren 
Java :: array of number arrays java 
Java :: abstraction in java 
Java :: java add element to existing array 
Java :: how to use a combo box in java with if else 
Java :: creating a menu in java 
Java :: difference between class and interface 
Java :: java program to Check given String is contians number or not 
Java :: java iterator index 
Java :: find last element in array in java 
Java :: print double without decimal java 
Java :: important features of java programming language 
Java :: Java print() and println() 
Java :: how to get elements of a list in java 
Java :: list java 
Java :: how to replace an element in array in java 
Java :: regex double quote java 
Java :: why enable multidex android 
Java :: android dialog modal 
Java :: list constructor java 
Java :: java run jar with args 
Java :: Infinite While Loop Java Example 
Java :: java treemap 
Java :: round decimals in java string 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =