How to read and write a file in java?

This article will show how we can read and write files from user input.

Java program to read a file line by line and display output:

Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file, and the length of the file in bytes.

Algorithm: Java program to read file

  1. import java.io.*; package
  2. create class Filedemo{
  3. Create function: public static void p(String str){
    1. Print String: System.out.println(str);
  4. Againg create another function: public static void analyze(String s){
    1. File f = new File(s)’
  5. if( f.exists()){
    1. p(f.getName()+” is a file”);
    2.  p(f.canRead()?” is readable”:” is not readable”);

    3. p(f.canWrite()?” is writable”:” is not writable”);

    4. p(“Filesize:”+f.length()+” bytes”);

    5. p(“File last mdified:”+f.lastModified());

  6.    if(f.isDirectory()) then

    1. p(f.getName()+” is directory”);

    2. p(“List of files”);

    3. String dir[]=f.list();

    4.  for(int i=0;i<dir.length;i++)

    5. p(dir[i])

  7. Create main function: public static void main(String rr[])throws IOException{
    1. filedemo fd=new filedemo();
    2. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    3. System.out.println(“Enter the file name:”);
    4. String s=br.readLine();
    5. fd.analyze(s);

Source Code: File Readable | File Writeable | Types of file | Size of file

import java.io.*;
class filedemo
{
public static void p(String str)
{
System.out.println(str);
}
public static void analyze(String s)
File f=new File(s);
if(f.exists())
{
p(f.getName()+" is a file");
p(f.canRead()?" is readable":" is not readable");
p(f.canWrite()?" is writable":" is not writable");
p("Filesize:"+f.length()+" bytes");
p("File last mdified:"+f.lastModified());
}
if(f.isDirectory())
{
p(f.getName()+" is directory");
p("List of files");
String dir[]=f.list();
for(int i=0;i<dir.length;i++)
p(dir[i]);
}
}

}
public class FileDetails
{
public static void main(String rr[])throws IOException
{
filedemo fd=new filedemo();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file name:");
String s=br.readLine();
fd.analyze(s);
}
}

Expected Output:

D:java>java FileDetails
Enter the file name:
AA.java
AA.java is a file
 is readable
 is writable
Filesize:95 bytes
File last mdified:1549353789940
write a java program that reads a file name from the user then display

Recommended Post:

Get Salesforce Answers

Tags:

java file reader,java file,java filereader,java filewriter,java fileinputstream,java file to inputstream,java file class,java file delete,java file io,java file copy,java file handling,java file exists,java file path,java file append,java file move,java file to byte array,java file separator,java file to string,java file get size,java file import,java file encoding,java file chooser,java file from inputstream,java file lastmodified,java file bufferedreader,java file handling programs,java file browser,java file attributes,

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment