A
7 8
请问,我要读取这三行数据,用BuffereReader,应该怎么写
如果是 56 38 A 7 8 写法我会,如果多行是否要建多个BufferedReader对象?????
8 个解决方案
#1
readLine()
#2
import java.io.*;
import java.util.StringTokenizer;
public class Test11
{
public static void main(String[] args) throws IOException,FileNotFoundException
{
int x;
char ch;
int y;
int x1,x2,x3,x4;
String str;
StringTokenizer tokenizer;
BufferedReader inFile=new BufferedReader(new FileReader("E:\\abcd\\inData.txt"));
PrintWriter outFile=new PrintWriter(new FileWriter("E:\\abcd\\outData.txt"));
tokenizer=new StringTokenizer(inFile.readLine());
x1=Integer.parseInt(tokenizer.nextToken());
x2=Integer.parseInt(tokenizer.nextToken());
str=tokenizer.nextToken();
ch=str.charAt(0);
x3=Integer.parseInt(tokenizer.nextToken());
x4=Integer.parseInt(tokenizer.nextToken());
x=x1 + x2;
ch=(char)(ch+1);
y=x3 * x4;
outFile.println("The sum of 56 and 38 ="+x+
"\n"+"The character that comes after A in the Unicode set is "+ch+
"\n"+"The product of 7 and 8="+y);
outFile.close();
}
}
我就是用readLine()不行,弹出例外.
import java.util.StringTokenizer;
public class Test11
{
public static void main(String[] args) throws IOException,FileNotFoundException
{
int x;
char ch;
int y;
int x1,x2,x3,x4;
String str;
StringTokenizer tokenizer;
BufferedReader inFile=new BufferedReader(new FileReader("E:\\abcd\\inData.txt"));
PrintWriter outFile=new PrintWriter(new FileWriter("E:\\abcd\\outData.txt"));
tokenizer=new StringTokenizer(inFile.readLine());
x1=Integer.parseInt(tokenizer.nextToken());
x2=Integer.parseInt(tokenizer.nextToken());
str=tokenizer.nextToken();
ch=str.charAt(0);
x3=Integer.parseInt(tokenizer.nextToken());
x4=Integer.parseInt(tokenizer.nextToken());
x=x1 + x2;
ch=(char)(ch+1);
y=x3 * x4;
outFile.println("The sum of 56 and 38 ="+x+
"\n"+"The character that comes after A in the Unicode set is "+ch+
"\n"+"The product of 7 and 8="+y);
outFile.close();
}
}
我就是用readLine()不行,弹出例外.
#3
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));
int x1,x2,x3,x4,x5;
char c;
x1=in.readInt();
x2=in.readInt();
x3=in.readInt();
x4=in.readInt();
x5=in.readInt();
c=(char)x3;
//读出来了吧。
}
}
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));
int x1,x2,x3,x4,x5;
char c;
x1=in.readInt();
x2=in.readInt();
x3=in.readInt();
x4=in.readInt();
x5=in.readInt();
c=(char)x3;
//读出来了吧。
}
}
#4
运行的时候好象BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));这一句有错误也!大哥问题出在那啊?谢谢赐教!
#5
x:\xx.txt 要输入真确的路径
比如你要读取c盘下的read.txt文件,那么就是:
BufferedInputStream in=new BufferedInputStream(new FileInputStream("c:\\read.txt"));
比如你要读取c盘下的read.txt文件,那么就是:
BufferedInputStream in=new BufferedInputStream(new FileInputStream("c:\\read.txt"));
#6
up
#7
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedReader in=new BufferedReader(new FileReader("x:\\xx\\xx\\x\\xx\\xx\\111.txt"));
String x1,x2,x3,x4,x5;
char c;
x1=in.readLine();
x2=in.readLine();
x3=in.readLine();
x4=in.readLine();
x5=in.readLine();
System.out.println(x3);
//c=(char)x3;
//读出来了吧。
}
}
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedReader in=new BufferedReader(new FileReader("x:\\xx\\xx\\x\\xx\\xx\\111.txt"));
String x1,x2,x3,x4,x5;
char c;
x1=in.readLine();
x2=in.readLine();
x3=in.readLine();
x4=in.readLine();
x5=in.readLine();
System.out.println(x3);
//c=(char)x3;
//读出来了吧。
}
}
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
#8
还有个问题,readLine()与readInt()用法上有什么区别吗?
readLine()是读取一行!返回String字符串
readInt()? 不是很明白!...知道是返回int类型
readLine()是读取一行!返回String字符串
readInt()? 不是很明白!...知道是返回int类型
#1
readLine()
#2
import java.io.*;
import java.util.StringTokenizer;
public class Test11
{
public static void main(String[] args) throws IOException,FileNotFoundException
{
int x;
char ch;
int y;
int x1,x2,x3,x4;
String str;
StringTokenizer tokenizer;
BufferedReader inFile=new BufferedReader(new FileReader("E:\\abcd\\inData.txt"));
PrintWriter outFile=new PrintWriter(new FileWriter("E:\\abcd\\outData.txt"));
tokenizer=new StringTokenizer(inFile.readLine());
x1=Integer.parseInt(tokenizer.nextToken());
x2=Integer.parseInt(tokenizer.nextToken());
str=tokenizer.nextToken();
ch=str.charAt(0);
x3=Integer.parseInt(tokenizer.nextToken());
x4=Integer.parseInt(tokenizer.nextToken());
x=x1 + x2;
ch=(char)(ch+1);
y=x3 * x4;
outFile.println("The sum of 56 and 38 ="+x+
"\n"+"The character that comes after A in the Unicode set is "+ch+
"\n"+"The product of 7 and 8="+y);
outFile.close();
}
}
我就是用readLine()不行,弹出例外.
import java.util.StringTokenizer;
public class Test11
{
public static void main(String[] args) throws IOException,FileNotFoundException
{
int x;
char ch;
int y;
int x1,x2,x3,x4;
String str;
StringTokenizer tokenizer;
BufferedReader inFile=new BufferedReader(new FileReader("E:\\abcd\\inData.txt"));
PrintWriter outFile=new PrintWriter(new FileWriter("E:\\abcd\\outData.txt"));
tokenizer=new StringTokenizer(inFile.readLine());
x1=Integer.parseInt(tokenizer.nextToken());
x2=Integer.parseInt(tokenizer.nextToken());
str=tokenizer.nextToken();
ch=str.charAt(0);
x3=Integer.parseInt(tokenizer.nextToken());
x4=Integer.parseInt(tokenizer.nextToken());
x=x1 + x2;
ch=(char)(ch+1);
y=x3 * x4;
outFile.println("The sum of 56 and 38 ="+x+
"\n"+"The character that comes after A in the Unicode set is "+ch+
"\n"+"The product of 7 and 8="+y);
outFile.close();
}
}
我就是用readLine()不行,弹出例外.
#3
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));
int x1,x2,x3,x4,x5;
char c;
x1=in.readInt();
x2=in.readInt();
x3=in.readInt();
x4=in.readInt();
x5=in.readInt();
c=(char)x3;
//读出来了吧。
}
}
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));
int x1,x2,x3,x4,x5;
char c;
x1=in.readInt();
x2=in.readInt();
x3=in.readInt();
x4=in.readInt();
x5=in.readInt();
c=(char)x3;
//读出来了吧。
}
}
#4
运行的时候好象BufferedInputStream in=new BufferedInputStream(new FileInputStream("x:\xx.txt"));这一句有错误也!大哥问题出在那啊?谢谢赐教!
#5
x:\xx.txt 要输入真确的路径
比如你要读取c盘下的read.txt文件,那么就是:
BufferedInputStream in=new BufferedInputStream(new FileInputStream("c:\\read.txt"));
比如你要读取c盘下的read.txt文件,那么就是:
BufferedInputStream in=new BufferedInputStream(new FileInputStream("c:\\read.txt"));
#6
up
#7
import java.io.*;
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedReader in=new BufferedReader(new FileReader("x:\\xx\\xx\\x\\xx\\xx\\111.txt"));
String x1,x2,x3,x4,x5;
char c;
x1=in.readLine();
x2=in.readLine();
x3=in.readLine();
x4=in.readLine();
x5=in.readLine();
System.out.println(x3);
//c=(char)x3;
//读出来了吧。
}
}
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
import java.util.*;
public class test
{
public static void main(String[] args) throws IOException,ClassNotFoundException{
BufferedReader in=new BufferedReader(new FileReader("x:\\xx\\xx\\x\\xx\\xx\\111.txt"));
String x1,x2,x3,x4,x5;
char c;
x1=in.readLine();
x2=in.readLine();
x3=in.readLine();
x4=in.readLine();
x5=in.readLine();
System.out.println(x3);
//c=(char)x3;
//读出来了吧。
}
}
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
#8
还有个问题,readLine()与readInt()用法上有什么区别吗?
readLine()是读取一行!返回String字符串
readInt()? 不是很明白!...知道是返回int类型
readLine()是读取一行!返回String字符串
readInt()? 不是很明白!...知道是返回int类型