0

I have used File.createTempFile() method to create temp file but as its output it appends the garbage value with the file name too. I used the method for uploading zipfile, but unable to delete those appended garbage value. For further functionality I need the exact name of file.

Kindly help...


Highly appreciate your response. My concern is, as code stated by niiraj874u, I am getting the the File name : tmp4501156806082176909.txt But I want only tmp.txt How can I remove appended numeric value?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
priya23
  • 21
  • 4
  • Please post the code, but not all of it, just the part where you have isolated the problem. It should be quite easy to remove but without any code I am a little confused as to what the problem exactly is. – CodeCamper Apr 16 '14 at 12:21
  • You mean it generated a "random" file name for you? Is that your problem or is the problem how to get the filename generated? – Peter Svensson Apr 16 '14 at 12:22
  • possible duplicate of [How to remove garbage value from a file name which has been created by using createTempFile() method? I need only file name with ext](http://stackoverflow.com/questions/23192701/how-to-remove-garbage-value-from-a-file-name-which-has-been-created-by-using-cre) – Pang Apr 26 '14 at 02:22

2 Answers2

0

You can use java.io.File.getName() method to get name of file..

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

public class FileDemo {
   public static void main(String[] args) {
      
       File f = null;
            
         // creates temporary file
         try {
            f = File.createTempFile("tmp", ".txt", new File("D:/"));
        } catch (IOException e) {
            e.printStackTrace();
        }
         
         // prints name of temp file
         System.out.println("File name: "+f.getName());
     // prints absolute path
     System.out.println("File path: "+f.getAbsolutePath());
   }
}

this will print like

File name: tmp4501156806082176909.txt

File path: D:\tmp4501156806082176909.txt

Community
  • 1
  • 1
niiraj874u
  • 2,180
  • 1
  • 12
  • 19
0

It sounds like you don't need a temp file. The purpose of the "garbage" is to protect two or more instances of the app from overwriting each other. In this case use system.getProperty("java.io.temp") to get the temp dir.

Virmundi
  • 2,497
  • 3
  • 25
  • 34