C#与JAVA中的readline及While问题
我有一个文本文件,想用while每次取一行。
在C#中,第一个while循环里是从第一行开始的,而在JAVA里则是从第二行开始的。
我只是试验了一下,具体原因还是不清楚,还望知情人出来说明一下。谢谢。
代码如下:
view plaincopy to clipboardprint?
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
StreamReader reader = new StreamReader(@"d:\javawork\mb.hua", System.Text.Encoding.Default);
string input = textBox1.Text;
string Line = reader.ReadLine();
while (Line != null)
{
if (Line.StartsWith(input))
{
MessageBox.Show(Line.Substring(1, 4));
}
Line = reader.ReadLine();
}
}
catch (Exception e1)
{
MessageBox.Show("发生错误,反馈问题请访问http://anforen.5d6d.com/");
}
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
StreamReader reader = new StreamReader(@"d:\javawork\mb.hua", System.Text.Encoding.Default);
string input = textBox1.Text;
string Line = reader.ReadLine();
while (Line != null)
{
if (Line.StartsWith(input))
{
MessageBox.Show(Line.Substring(1, 4));
}
Line = reader.ReadLine();
}
}
catch (Exception e1)
{
MessageBox.Show("发生错误,反馈问题请访问http://anforen.5d6d.com/");
}
}
}
}
上面是C#版的。
下面是java版的:
package fourcorner.server;
import java.io.*;
import fourcorner.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) {
try{
FileReader fr=new FileReader("WEB-INF/MB.hua");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();
while(Line!=null){
if(Line.startsWith(input))
{
return Line.substring(1, 5);
}
Line=br.readLine();
}
if(Line==null)
return "请输入一个汉字,反馈问题请访问http://anforen.5d6d.com/";
br.close();
fr.close();
return Line.toString();
}
catch(Exception e)
{
return "发生错误,反馈问题请访问http://anforen.5d6d.com/";
}
}
}
package fourcorner.server;
import java.io.*;
import fourcorner.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) {
try{
FileReader fr=new FileReader("WEB-INF/MB.hua");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();
while(Line!=null){
if(Line.startsWith(input))
{
return Line.substring(1, 5);
}
Line=br.readLine();
}
if(Line==null)
return "请输入一个汉字,反馈问题请访问http://anforen.5d6d.com/";
br.close();
fr.close();
return Line.toString();
}
catch(Exception e)
{
return "发生错误,反馈问题请访问http://anforen.5d6d.com/";
}
}
}
import java.io.*;
import fourcorner.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) {
try{
FileReader fr=new FileReader("WEB-INF/MB.hua");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();
while(Line!=null){
if(Line.startsWith(input))
{
return Line.substring(1, 5);
}
Line=br.readLine();
}
if(Line==null)
return "请输入一个汉字,反馈问题请访问http://anforen.5d6d.com/";
br.close();
fr.close();
return Line.toString();
}
catch(Exception e)
{
return "发生错误,反馈问题请访问http://anforen.5d6d.com/";
}
}
}
package fourcorner.server;
import java.io.*;
import fourcorner.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) {
try{
FileReader fr=new FileReader("WEB-INF/MB.hua");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();
while(Line!=null){
if(Line.startsWith(input))
{
return Line.substring(1, 5);
}
Line=br.readLine();
}
if(Line==null)
return "请输入一个汉字,反馈问题请访问http://anforen.5d6d.com/";
br.close();
fr.close();
return Line.toString();
}
catch(Exception e)
{
return "发生错误,反馈问题请访问http://anforen.5d6d.com/";
}
}
}