How to set file
permissions in java
package com.java2novice.files;
import java.io.File;
public class MyFilePermissions
{
public static void main(String a[])
{
File
scriptFile = new File("/home/java2novice/test.sh");
System.out.println("Current
file permissions:");
System.out.println("Can
Execute? "+scriptFile.canExecute());
System.out.println("Can
Read? "+scriptFile.canRead());
System.out.println("Can
Write? "+scriptFile.canWrite());
scriptFile.setExecutable(true);
scriptFile.setReadable(true);
scriptFile.setWritable(true);
System.out.println("Now
file permissions:");
System.out.println("Can
Execute? "+scriptFile.canExecute());
System.out.println("Can
Read? "+scriptFile.canRead());
System.out.println("Can
Write? "+scriptFile.canWrite());
}
}
How to read a file
using Buffered Input Stream
package com.java2novice.files;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class MyFileReadBybis
{
public static void main(String a[])
{
InputStream
is = null;
BufferedInputStream
bis = null;
DataInputStream
dis = null;
try
{
is
= new
FileInputStream("/Users/ngootooru/sample.txt");
bis
= new
BufferedInputStream(is);
dis
= new
DataInputStream(bis);
String
temp = null;
while((temp
= dis.readLine()) != null)
{
System.out.println(temp);
}
}
catch (FileNotFoundException
e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
How to create
temporary file in java
package com.java2novice.files;
import java.io.File;
import java.io.IOException;
public class MyTmpFile
{
public static void main(String a[])
{
File
tmpFile = null;
try
{
tmpFile
= File.createTempFile("MyTempFile", ".tmp");
System.out.println("Created
Temp File Location : " + tmpFile.getAbsolutePath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
How to write or
store data into temporary file in java
package com.java2novice.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MyTempFileWrite
{
public static void main(String a[])
{
File tempFile = null;
BufferedWriter
writer = null;
try
{
tempFile
= File.createTempFile("MyTempFile", ".tmp");
writer
= new
BufferedWriter(new FileWriter(tempFile));
writer.write("Writing
data into temp file!!!");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(writer
!= null) writer.close();
}
catch(Exception ex){}
}
System.out.println("Stored
data in temporary file.");
}
}
No comments:
Post a Comment