menu


Subscribe to the blog

Social networks

My page on vk.com
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Java - TXT Read and write a text document

Programming Date of creation: Views: 282

Java - TXT Read and write a text document

lesnih.info


Russia

Reading a text document in Java

import java.io.*;
 
public class text {
 
    public static void main(String[] args) {
        
        try(FileReader reader = new FileReader("C:\NewFolder\text.txt")) // Specify the path where the file is located
        {
           // read
            int ax;
            while((ax=reader.read())!=-1){
                 
                System.out.print((char)ax);
            } 
        }
        catch(IOException ex){
             
            System.out.println(ex.getMessage());
        }   
    } 
}


Write to a text document in Java

import java.io.*;
 
public class text {
 
    public static void main(String[] args) {
        
        try(FileWriter writer = new FileWriter("C:\NewFolder\text.txt", false))
        {
           // write the entire line
            String text = "Hello world!";
            writer.write(text);
            
             
            writer.flush();
        }
        catch(IOException ex){
             
            System.out.println(ex.getMessage());
        } 
    } 
}


Where is the compiled file you can read in another of my articles: How to compile a file in the NetBeans IDE

The text.txt file appears in the same folder where the compiled file itself is located.

Accordingly, the message specified in the code is displayed:

Hello world!


Java - TXT Read and write a text document
Rating: 5 | Тotal votes: 2 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    Java - TXT Read and write a text document

    Java - TXT Read and write a text document
    Topic: Programming

    Tags: java,txt,write txt,read txt,integration,exchange

    Views:282

    Java XML - XML record in a java application

    Java XML - XML record in a java application
    Topic: Programming

    Tags: java,xml,write xml,integration,exchange

    Views:1261

    Post java - Send POST request in Java

    Post java - Send POST request in Java
    Topic: Programming

    Tags: java,post,php,integration,exchange

    Views:326