Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java打印方形(未知字符)和常规文本_Java_File_Csv - Fatal编程技术网

Java打印方形(未知字符)和常规文本

Java打印方形(未知字符)和常规文本,java,file,csv,Java,File,Csv,代码: public class MainApplication { public static void main(String[] args) throws IOException { try{ // Open the file that is the first // command line parameter FileInputStr

代码:

public class MainApplication {

      public static void main(String[] args) throws IOException {

              try{
                  // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("data/temp.CSV");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                  String strLine;
                  //Read File Line By Line
                  while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  System.out.println (strLine);
                  }
                  //Close the input stream
                  in.close();
                    }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                  }
      }
}
19/1/13 13:58:04    0   1610    0   419 0   0
19/1/13 13:58:05    0.01    1599    66  432 0   1
19/1/13 13:58:06    0.02    1603    47  423 0   2
19/1/13 13:58:07    0.03    1602    26  413 0   3
19/1/13 13:58:08    0.04    1605    130 412 0   4
CSV文件数据:

public class MainApplication {

      public static void main(String[] args) throws IOException {

              try{
                  // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("data/temp.CSV");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                  String strLine;
                  //Read File Line By Line
                  while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  System.out.println (strLine);
                  }
                  //Close the input stream
                  in.close();
                    }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                  }
      }
}
19/1/13 13:58:04    0   1610    0   419 0   0
19/1/13 13:58:05    0.01    1599    66  432 0   1
19/1/13 13:58:06    0.02    1603    47  423 0   2
19/1/13 13:58:07    0.03    1602    26  413 0   3
19/1/13 13:58:08    0.04    1605    130 412 0   4
输出:

public class MainApplication {

      public static void main(String[] args) throws IOException {

              try{
                  // Open the file that is the first 
                  // command line parameter
                  FileInputStream fstream = new FileInputStream("data/temp.CSV");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                  String strLine;
                  //Read File Line By Line
                  while ((strLine = br.readLine()) != null)   {
                  // Print the content on the console
                  System.out.println (strLine);
                  }
                  //Close the input stream
                  in.close();
                    }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                  }
      }
}
19/1/13 13:58:04    0   1610    0   419 0   0
19/1/13 13:58:05    0.01    1599    66  432 0   1
19/1/13 13:58:06    0.02    1603    47  423 0   2
19/1/13 13:58:07    0.03    1602    26  413 0   3
19/1/13 13:58:08    0.04    1605    130 412 0   4

更换

BufferedReader br = new BufferedReader(new InputStreamReader(in));

.csv
文件的编码替换
“UTF-8”

使用
InputStreamReader的此构造函数
可以根据给定的字符集正确处理输入。如果未指定字符集且输出奇怪,则表明文件的编码方式与系统默认编码方式不同

此外,您还可以去掉
DataInputStream
并编写

BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-16"));
编辑感谢Henry指出以下几点:

查看您的输出,每个字符似乎都使用2个字节进行编码。这表明它的编码是UTF-16。您应该相应地在构造函数中使用“UTF-16”。

使用

BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-16LE"));
而不是

BufferedReader br = new BufferedReader(new InputStreamReader(in));

使用的编码可能是UTF-16。从输出来看,它是一些16位编码(正确的字符与未知字符混合,可能是\000)是的,谢谢。这是有效的。我试过UTF-8,但不起作用。不要对字符集使用字符串文字。尽可能使用
StandardCharsets
。因此,您将用
标准字符集.UTF\u 16LE
替换
“UTF-16LE”
。请不要使用DataInputStream读取文本