0

I am trying to create a temporary files using java.But when i run my codes it shows the following error.

number.java:7: cannot find symbol
symbol  : class Path
location: class number
    Path tempFile = Files.createTempFile(null, ".txt");
    ^
number.java:7: cannot find symbol
symbol  : method createTempFile(<nulltype>,java.lang.String)
location: class Files
    Path tempFile = Files.createTempFile(null, ".txt");
                         ^
2 errors

and here's the code and when i import java.io.file;.then it show error package does not exist

import java.io.*;


public class number{
public static void main(String args[])
{try {
    Path tempFile = Files.createTempFile(null, ".txt");
    System.out.format("The temporary file" +
        " has been created: %s%n", tempFile);
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
}
}}
Gaurav
  • 69
  • 1
  • 6
  • See this: http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java – Miguel Prz Apr 01 '13 at 07:12
  • in which package `Files` comes? It should have been `File` I guess. – AmitG Apr 01 '13 at 07:14
  • After upgrading to SE7 the error is Desktop\Jdbc\number.java:7: error: cannot find symbol Path tempFile = Files.createTempFile(temp, ".txt"); ^ symbol: variable temp location: class number 1 error – Gaurav Apr 01 '13 at 08:23

2 Answers2

1

Files.createTempFile(null, ".txt"); 1st parameter cant be null, it can be any prefix (basically to identify your temp file among lots of other). pass it some non null values.

Files.createTempFile("myfile", ".txt");

Also, correct import as below

import java.nio.file.*;
import java.io.*;
Ankit
  • 6,554
  • 6
  • 49
  • 71
  • what should i pass in place of null – Gaurav Apr 01 '13 at 07:12
  • After changing this number.java:7: cannot find symbol symbol : class Path location: class number Path tempFile = Files.createTempFile(temp, ".txt"); ^ number.java:7: cannot find symbol symbol : variable temp location: class number Path tempFile = Files.createTempFile(temp, ".txt"); ^ 2 errors – Gaurav Apr 01 '13 at 07:15
  • when i import java.io.file.Files error is package java.io.file does not exist – Gaurav Apr 01 '13 at 07:18
  • yes, bcz its under java.nio.file, not java.io.file – Ankit Apr 01 '13 at 07:21
  • java.io.file also exist http://www.roseindia.net/java/java-tips/io/10file/05file.shtml – Gaurav Apr 01 '13 at 07:23
  • @Gaurav yes, but you are mixing it up. Path & Files are in java7. Better decide what you wanna use. Under java 7 or below..... check the answer to correct the imports – Ankit Apr 01 '13 at 07:29
0

The Path class, introduced in the Java SE 7 release, is one of the primary entrypoints of the java.nio.file package. If your application uses file I/O, you will want to learn about the powerful features of this class. See this for more info: http://docs.oracle.com/javase/tutorial/essential/io/pathClass.html

http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html

Civa
  • 2,058
  • 2
  • 18
  • 30