java 如何获取文件的换行符

现在需要得到文件的换行符 但是使用BufferReader .readLine()读取一行时是忽略了换行符的 怎么使用BufferReader .read()得到文件的换行符或者是其他方式得到文件的换行符

import java.io.FileReader;
import java.io.IOException;

public class FileReadWrite
{
public static void main(String[] args)
{
FileReader reader = null;
try
{
// 选用指定文件路径的构造方法
reader = new FileReader("test.txt");
int data = -1;
// 从此输入流中读取一个数据字节,如果已到达文件末尾,则返回 -1
while ((data = reader.read()) != -1)
{
// 准备的文件为文本文件,且内容为阿拉伯数字,所以直接强制转换为字符输出
System.out.print((char) data);
}
}
catch (IOException e)
{
// 异常处理
e.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
// 关闭输入流
reader.close();
}
}
catch (IOException e)
{
}
}
}
}

这样一个个字符读取 就不会漏了换行符

import java.io.FileReader;
import java.io.IOException;

public class FileReadWrite
{
public static void main(String[] args)
{
FileReader reader = null;
try
{
// 构造文件
reader = new FileReader("test.txt");
int data = -1;
// 读取一个数据字节,如果已到达文件末尾,则返回 -1
while ((data = reader.read()) != -1)
{
// 准备的文件为文本文件,且内容为阿拉伯数字,所以直接强制转换为字符输出
System.out.print((char) data);
}
}
catch (IOException e)
{
// 异常处理
e.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
// 关闭输入流
reader.close();
}
}
catch (IOException e)
{
}
}
}
}

这样一个个字符读取 就不会漏了换行符

自己写的工具类,希望适用

   /**
   * 按行读取文件
   * @param file
   * @param fromLineNum
   * @param charset
   * @return
   */
  public static List<String> readLines(File file,int fromLineNum,Charset charset) {
    List<String> lines=new ArrayList<String>();
    int prechar=-1;
    int lineNum=0;
    InputStream is=null;
    try {
      is=new FileInputStream(file);
      int data=0;
      List<Byte> bList=new ArrayList<Byte>();
      while((data=is.read())!=-1) {
        byte x=(byte) (data>127?data-256:data);
        if(data=='\n') {
         lineNum+=1;
         if(prechar=='\r') {
           bList.remove(bList.size()-1);
         }
         byte bs[]=new byte[bList.size()];
         for(int i=0;i<bList.size();i++) {
           bs[i]=bList.get(i);
         }
         if(lineNum>=fromLineNum) {
           String line=new String(bs,0,bs.length,charset);
           lines.add(line);
         }
         bList.clear();
        }else {
          bList.add(x);
        }
        prechar=data;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(is!=null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return lines;
  }

利用ASCII码获取 可以直接用CHAR类型输入ASCII码值

你还是对字符编码吧,.readLine()遇到换行会被当做结束符的