Showing posts with label Files. Show all posts
Showing posts with label Files. Show all posts

How to find temporary file path using java

How to find temporary file path using java

import java.io.File;
import java.io.IOException;

public class GetTempFilePath
{
public static void main(String[] args)
{

try{

//create a temp file
File temp = File.createTempFile(tempfile", ".tmp");

System.out.println("Temp file : " + temp.getAbsolutePath());

//Get tempropary file path
String absolutePath = temp.getAbsolutePath();
String tempFilePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));

System.out.println("Temp file path : " + tempFilePath);

}catch(IOException e){

e.printStackTrace();

}

}
}

 

How to delete temporary files using Java

How to delete temporary files using Java

import java.io.File;
import java.io.IOException;

public class DeleteTempFileExample
{
public static void main(String[] args)
{

try{

//create a temp file
File temp = File.createTempFile("tempfile", ".tmp");

//delete temporary file when the program is exited
temp.deleteOnExit();

//delete immediate
//temp.delete();

}catch(IOException e){

e.printStackTrace();

}

}
}

 

How to write data into temporary file

How to write data into temporary file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteTempFileExample
{
public static void main(String[] args)
{

try{

//create a temp file
File temp = File.createTempFile("tempfile", ".tmp");

//write it
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write("This is the temporary file content");
bw.close();

System.out.println("Done");

}catch(IOException e){

e.printStackTrace();

}

}
}

 

How to create temporary files in java

How to create temporary files in java

The follwoing explains about , how to create temporary file


import java.io.File;
import java.io.IOException;

public class CreateTempFileExample
{
public static void main(String[] args)
{

try{

//create a temp file
File temp = File.createTempFile("temporaryfile", ".tmp");

System.out.println("Temp file : " + temp.getAbsolutePath());

}catch(IOException e){

e.printStackTrace();

}

}
}

 

How to set and change file permission using java

How to set and change file permission using java

The following list method which support to check and change file permission using Java API

Check if the file permission allow :

  1. file.canExecute(); – return true, file is executable; false is not.
  2. file.canWrite(); – return true, file is writable; false is not.
  3. file.canRead(); – return true, file is readable; false is not.

Set the file permission :

  1. file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
  2. file.setReadable(boolean); – true, allow read operations; false to disallow it

import java.io.File;
import java.io.IOException;

public class FilePermissionDemo
{
public static void main( String[] args )
{
try {

File file = new File("/mkyong/shellscript.sh");

if(file.exists()){
System.out.println("Is Execute allow : " + file.canExecute());
System.out.println("Is Write allow : " + file.canWrite());
System.out.println("Is Read allow : " + file.canRead());
}

file.setExecutable(false);
file.setReadable(false);
file.setWritable(false);

System.out.println("Is Execute allow : " + file.canExecute());
System.out.println("Is Write allow : " + file.canWrite());
System.out.println("Is Read allow : " + file.canRead());

if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

 

How to find a line with Maximum length from a File contains data

How to find a line with Maximum length from a File contains data.
SOURCE : MaxLineWordFromFile.java
INPUT

I have stored a file at below location
C:\\new\\testing.txt
testing.txt file contains :---
this is my india and all indian are my ,,,,
all indians are like brothers and sisters and so we can say this our
hello here iamhey be aware of dogs and human
hello
you see!!! what exactly you are talking about



OUTPUT

longest line : all indians are like brothers and sisters and so we can say this our
14







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+