-8

I have filepath and title of the file. Is there a way to create a File object with these informations ?

Something like this:

String a = new String("/directory/name.txt");
String b = new String("This is the title");

File f = new File(a,b);
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • You can't single quote strings in Java. Try `String a = "/directory/name.txt"` and `String b = "This is the title"` – Eli Sadoff Nov 04 '16 at 17:33
  • @EliSadoff sorry, it was an error of distraction – xRobot Nov 04 '16 at 17:34
  • 3
    There's also no reason to use the `new String` construct as it's no faster. – Eli Sadoff Nov 04 '16 at 17:34
  • 2
    Files don't have a title. What do you mean by "title". What would be, for example, the title of your Java file containing that source code? – JB Nizet Nov 04 '16 at 17:34
  • Also, `File f = new File(a,b)` will create a file `/directory/name.txt/"This is the title"`. – Eli Sadoff Nov 04 '16 at 17:35
  • What are you trying to achieve? Your title shows "convert strings in a File object"! Convert to what? – Alvin Bunk Nov 04 '16 at 17:37
  • 4
    Did you even bother to *look* at the javadoc of the `File` constructor you are calling? [`File(String parent, String child)`](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.lang.String-java.lang.String-) What do you believe `parent` and `child` means? I mean, you could *read* the javadoc, which does explain it, but just look at the names. Do either of those imply "title" to you? – Andreas Nov 04 '16 at 17:37

1 Answers1

1

File has a constructor which receives a string of the location of that file on the computer.

File f = new File("path\\to\\your\\file");

Assuming that title of the file means its name, you can use

f.renameTo("newName");

to rename your file.

Unrelated to files: You don't need to initialize a String. just write:

String a = "/directory/name.txt";

to create a string.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • @cricket_007 I felt it relevant here as I am adding to the answer, so it would fit to the question better. questions are not exactly the same, so there is room for adding – ItamarG3 Nov 04 '16 at 17:38
  • @cricket_007 I think that the answer is good, as it covers more of the question. – ItamarG3 Nov 04 '16 at 17:42