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:
Algorithm: Java program to read file
- import java.io.*; package
- create class Filedemo{
- Create function: public static void p(String str){
- Print String: System.out.println(str);
- Againg create another function: 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()) then
p(f.getName()+” is directory”);
p(“List of files”);
String dir[]=f.list();
for(int i=0;i<dir.length;i++)
p(dir[i])
- Create main function: 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);
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:
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
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,