实现的功能是读取文本文件的最后一行:
try
{
File file=new File("filename.txt");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
int len=(int)file.length();
boolean flag=true;
int num_temp=0;
//String str=null;
char chars[]=new char[1000];/*不一定是len,可以定义一个比较大的数也可以。只要比估计的一行字符的个数要大就行!!!*/
char ch;
while(flag)
{
num_temp=br.read(chars,len,len--);
if(chars[0].equals("\n"))
flag=false;
}
num_temp=br.read(chars,len+1,file.length());
}
catch(IOException e)
{
System.out.println("IO Error!");
}
System.out.println("你的文件的最后一行的字符是:\n"+chars.toString());
try
{
File file=new File("filename.txt");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
int len=(int)file.length();
boolean flag=true;
int num_temp=0;
//String str=null;
char chars[]=new char[1000];/*不一定是len,可以定义一个比较大的数也可以。只要比估计的一行字符的个数要大就行!!!*/
char ch;
while(flag)
{
num_temp=br.read(chars,len,len--);
if(chars[0].equals("\n"))
flag=false;
}
num_temp=br.read(chars,len+1,file.length());
}
catch(IOException e)
{
System.out.println("IO Error!");
}
System.out.println("你的文件的最后一行的字符是:\n"+chars.toString());
解决方案 »
- c#里面结构体怎么处理
- "HANDLE WINAPI" 如何使用啊 ,我写老是报错啊. 这是初始化USB的程序有没有人用过啊
- 还是重写,哎~~
- 讨论
- 单词查询,以txt文本作为数据库的问题?
- 能否向其他应用程序中的文本框发送字符串?
- Cannot update. Database or object is read-only错误
- C# json 反序列化 求解
- 【菜鸟提问】问个弱弱的问题,如何在运行程序的时候,先出现一个公司的LOGO,在进入登录界面啊?
- DateTimePicker控件不好使用
- 其他信息: 试图在公开 MyRemoting.RemoteObj.FaxBusiness 的对象上调用类型 MyRemoting.ICommon.IFaxBusiness 上声明的方法。
- 大家看看这段代码哪里有问题???
try
{
System.IO.FileStream file=System.IO.File.OpenRead(@"c:\ss.txt"); System.IO.StreamReader sr = new System.IO.StreamReader(file);
bool flag = true;
do
{
string str = sr.ReadLine();
if (str!=null)
strLastLine = str;
else
flag = false; }while(flag); }
catch(System.IO.IOException ex)
{
Console.WriteLine("IO Error!");
}
Console.WriteLine("你的文件的最后一行的字符是:\n"+strLastLine);
FileStream fsMyfile = new FileStream(bFilePath, FileMode.Open, FileAccess.Read);
StreamReader srMyfile= new StreamReader(fsMyfile);
srMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
string key="";
int j=0;
while((key = srMyfile.ReadLine())!=null)
{
for(int i=0; i<key.Length; i++)
{
dataValue[j++] = key.ToString();
}
}
srMyfile.Close();
fsMyfile.Close();