0

Here are my code:

import java.io.Console;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
public class Welcome
{
   public static void main(String[] args) throws IOException
   {
       Scanner in=new Scanner(Paths.get("F:\\myfile.txt"),"UTF-8");
       String sentence=in.nextLine();  // I guess this line of code can read the first line of the txt in to the String sentence?
       System.out.print(sentence);
   }
}

and the content of myfile.txt is:

Eat an apple every day, keep the doctors away!

but the information displayed in the console window is:

content displayed in console window Why there was a symbol '?' before normal content?

Thanks! explain as readable as please, I'm not a native speaker.

Thomas
  • 87,414
  • 12
  • 119
  • 157
J.Butter
  • 57
  • 1
  • 6
  • Open your file with a hex Editor and see if there is a non printable character at the beginning of the file – Jens Oct 10 '17 at 11:37
  • Maybe it's not UTF-8 file format – Tuco Oct 10 '17 at 11:37
  • 1
    Open the file with a hex editor and look at the first 3 bytes. I'd say you'll see EF BB BF which is the [UTF-8 byte order mark](https://de.wikipedia.org/wiki/Byte_Order_Mark) (unless your file isn't encoded as UTF-8). – Thomas Oct 10 '17 at 11:38
  • This is most likely a IDE related issue I am guessing the ? is the cursor of the line for your IDE, or your operating system. This code works just fine in NetBeans8.2 and TextPad8 with Java8 Loaded. – Jeremiah Stillings Oct 10 '17 at 11:51
  • Thanks to everyone, I just found what the problem is. – J.Butter Oct 10 '17 at 12:17
  • the file was encoded in UTF-8 with BOM, so there was a mistake in the console window, I remove the BOM , then the program works well! – J.Butter Oct 10 '17 at 12:30

0 Answers0