-2

How would I get the ".txt" from displaying?

    File folder = new File("C:/Users/Camaloony/Desktop/Java Stuff/Python Home Control/Users/");
File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
            System.out.println(listOfFiles[i].getName());
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }

g.drawString(listOfFiles[0].getName(), 660, 395);

enter image description here

Thanks in advance for any help

Mickael
  • 3,506
  • 1
  • 21
  • 33
  • And what is your code so far? Without existing code you cannot really expect your question to be answered, right? – fge Sep 01 '16 at 18:08
  • 2
    "How would I [...]" => By writing some code. – Seelenvirtuose Sep 01 '16 at 18:08
  • I have code to generate the red squares depending on the amount of files in the directory etc... I just didn't know that it would be useful as it is pretty much unrelated to the actual names of the files themselves. – CameronOfoluwa Sep 01 '16 at 18:19
  • Turns out I found out how to get the names as well as the .txt -> how would I stop the .txt from dispalying - adding code now – CameronOfoluwa Sep 01 '16 at 18:23
  • You could split the string on the '.' and just use the first part of the split array. – Ringo Sep 01 '16 at 20:54

2 Answers2

1

If you want to get rid of the ".txt" when you call

 g.drawString(listOfFiles[0].getName(), 660, 395);

You can call the replace method on the string you get back from listOfFiles[0].getName() like so

g.drawString(listOfFiles[0].getName().replace(".txt", ""), 660, 395);

This is related to Remove part of string if you'd like to know more about the replace() method or you can check out https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)

Community
  • 1
  • 1
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0
    File f = new File("E:\\Code\\folder");
    File[] arr = f.listFiles();
    for (int i = 0; i < arr.length; i++) {
        String fileName = arr[i].getName().split("\\.")[0];
        System.out.println(fileName);   
    }

This will do the job of removing '.txt' :

   getName().split("\\.")[0]
Shaggy
  • 596
  • 1
  • 6
  • 15