import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Creater
{
public static void main(String ar[])
{
}
public static void createFolder(String folderNamePath)
{
File file = new File(folderNamePath);
file.mkdir();
}
public static void createBlankFile()
{
File f = new File("newfile.txt");
if(f.exists())
{
System.out.println("File already exist..!!");
}
else
{
try
{
f.createNewFile();
}
catch(IOException io) { }
}
}
public static void WriteFile(String fileName,String content) throws IOException
{
File f= new File(fileName); //string -> BufferedWriter -> FileWriter -> File
if(f.exists()) f.delete();
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
public static String ReadFile(String fileName) throws FileNotFoundException, IOException
{
FileInputStream fis=new FileInputStream(fileName); //string -> BufferedReader -> InputStreamReader -> DataInputStream -> FileInputStream
DataInputStream dis = new DataInputStream(fis);
InputStreamReader isr = new InputStreamReader(dis);
BufferedReader br = new BufferedReader(isr);
String text,fulltext="";
while((text=br.readLine())!=null) fulltext=fulltext+ text+"\n";
dis.close();
return fulltext;
}
public static void deleteFile(String fileName)
{
File f = new File(fileName);
f.delete();
}
public static void copyFile(String sourceFile,String newFile) throws FileNotFoundException, IOException
{
byte[] buf = new byte[1024];
File f = new File(sourceFile);
File f2 = new File(newFile);
InputStream is = new FileInputStream(f);
OutputStream os = new FileOutputStream(f2,true);
int len;
while((len=is.read(buf))>0)
{
os.write(buf,0,len);
}
is.close();
os.close();
System.out.print("Files Copies successfully");
}
public static void renameFile(String oldFileName,String newFileName)
{
File f = new File(oldFileName);
File f2 = new File(newFileName);
f.renameTo(f2);
}
public static String fileProperty(String fileName)
{
File f = new File("newfile.txt");
String properties="";
if(f.isFile()) properties+="It is a file..!!";
else if(f.isDirectory()) properties+="it is a directory..!!";
else if(f.isHidden()) properties+="it is a hidden file...!!";
else if(f.canRead()) properties+="Read permission allowed";
else if (f.canExecute()) properties+="Execute permission allowed";
else if(f.canWrite()) properties+="Write Permission allowed";
else if (f.exists()) properties+="Yes file exists";
return properties;
}
}
No comments:
Post a Comment