public class FileDemo {
public static void main(String[] args) {
File f = null;
String path = "";
boolean bool = false;
try {
// create new files
f = new File("test.txt");
// returns true if the file exists
bool = f.exists();
// if file exists
if(bool) {
// get absolute path
path = f.getAbsolutePath();
// prints
System.out.print("Absolute Pathname "+ path);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
public class FileGetAbsolutePathExample {
public static void main(String[] args) {
// initialize File object
File file = new File("input\\test.txt");
boolean result;
// check if file exists
result=file.exists();
if(result){
// print message that file exists
System.out.println(file.getAbsolutePath() + " exists");
}
else{
//print message that the file does not exist
System.out.println(file.getAbsolutePath()+" does not exists");
}
}
}
public class solution {
public static void main(String args[])
{
// try-catch block to handle exceptions
try {
// Create a file object
File f = new File("program.txt");
// Get the absolute path of file f
String absolute = f.getAbsolutePath();
// Display the file path of the file object
// and also the file path of absolute file
System.out.println("Original path: "
+ f.getPath());
System.out.println("Absolute path: "
+ absolute);
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
public class FileExample {
public static void main(String[] args) throws Exception {
try {
File file = new File("test/.././file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
} catch (Exception e) {
System.err.println("Things went wrong: " + e.getMessage());
e.printStackTrace();
} finally {
}
}
}
public class AbsoluteFilePathExample
{
public static void main(String[] args)
{
try{
File temp = File.createTempFile("i-am-a-temp-file", ".tmp" );
String absolutePath = temp.getAbsolutePath();
System.out.println("File path : " + absolutePath);
String filePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("File path : " + filePath);
}catch(IOException e){
e.printStackTrace();
}
}
}
public Request setDestinationInExternalFilesDir(Context context, String dirType,
String subPath) {
final File file = context.getExternalFilesDir(dirType);
if (file == null) {
throw new IllegalStateException("Failed to get external storage files directory");
} else if (file.exists()) {
if (!file.isDirectory()) {
throw new IllegalStateException(file.getAbsolutePath() +
" already exists and is not a directory");
}
} else {
if (!file.mkdirs()) {
throw new IllegalStateException("Unable to create directory: "+
file.getAbsolutePath());
}
}
setDestinationFromBase(file, subPath);
return this;
}
private void downloadFrom(Assignment toDown ) {
client.sendUTFDataToServer("DOWNLOAD_SUBS");
client.sendObjectToServer(toDown);
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(this);
File downFolder = chooser.getSelectedFile();
byte[] data = (byte[]) client.getObjectFromServer();
try {
fos = new FileOutputStream(downFolder.getAbsolutePath() + "/" + toDown.getName() + ".zip");
fos.write(data);
fos.close();
} catch (IOException ex) {
Logger.getLogger(StudentMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static boolean saveData2SDCardPrivateFileDir(byte[] data, String type, String filename, Context context) {
if (isSDCardMounted()) {
File path = context.getExternalFilesDir(type);
if (!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + File.separator + filename;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public File getChild(File parent, String fileName) {
if (fileName.startsWith("\\")
&& !fileName.startsWith("\\\\")
&& isFileSystem(parent)) {
//Path is relative to the root of parent's drive
String path = parent.getAbsolutePath();
if (path.length() >= 2
&& path.charAt(1) == ':'
&& Character.isLetter(path.charAt(0))) {
return createFileObject(path.substring(0, 2) + fileName);
}
}
return super.getChild(parent, fileName);
}
public static String searchDirForPath(File dir, int bucketId) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
String path = file.getAbsolutePath();
if (GalleryUtils.getBucketId(path) == bucketId) {
return path;
} else {
path = searchDirForPath(file, bucketId);
if (path != null) return path;
}
}
}
}
return null;
}