Java 随机获取文本文件值
我有一个文本文件:-Java 随机获取文本文件值,java,Java,我有一个文本文件:- 2,5,6,10,5,30 我正在尝试以下代码来读取文件:- package str; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; public class getall { /** * @param args */
2,5,6,10,5,30
我正在尝试以下代码来读取文件:-
package str;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class getall {
/**
* @param args
*/
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("D://Workspace_J/getstr/src/str/activity.text");
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());
}
for(int a =1;a<3;a = a+1)
{
System.out.println("get value from the file randomly");
}
}
}
包str;
导入java.io.BufferedReader;
导入java.io.DataInputStream;
导入java.io.FileInputStream;
导入java.io.InputStreamReader;
公共类getall{
/**
*@param args
*/
公共静态void main(字符串参数[])
{
试一试{
//打开第一个文件
//命令行参数
FileInputStream fstream=newfileinputstream(“D://Workspace\u J/getstr/src/str/activity.text”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
//在控制台上打印内容
System.out.println(strLine);
}
//关闭输入流
in.close();
}catch(异常e){//catch异常(如果有)
System.err.println(“错误:+e.getMessage());
}
对于(int a=1;a使用:
您可以使用String.split(“,”)
将值放入数组中,并使用Random.nextInt(array.length)
使用代码编辑:
public static void main(String args[])
{
try
{
...
// assuming your string is "2,5,6,10,5,30"
String[] tokens = strLine.split(",");
// tokens will be [2,5,6,10,5,30], size = 6
Random r = new Random();
for (int a = 1 ; a < 3 ; a++) //a++ is a short way of writing a = a + 1
{
int randomInt = r.nextInt(tokens.length); // an integer between 0 and 5
System.out.println(tokens[randomInt]);
}
}
...
}
publicstaticvoidmain(字符串参数[])
{
尝试
{
...
//假设字符串为“2,5,6,10,5,30”
String[]tokens=strLine.split(“,”);
//代币将为[2,5,6,10,5,30],大小=6
随机r=新随机();
for(inta=1;a<3;a++)//a++是编写a=a+1的一种简短方法
{
int randomInt=r.nextInt(tokens.length);//一个介于0和5之间的整数
System.out.println(令牌[randomInt]);
}
}
...
}
我想试试这个
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("D://Workspace_J/getstr/src/str/activity.text");
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);
}
String[] tokens = strLine.split(",");
for (int a = 1 ; a < 3 ; a++) //a++ is a short way of writing a = a + 1
{
System.out.println(tokens[Random.nextInt(tokens.length)]);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
试试看{
//打开第一个文件
//命令行参数
FileInputStream fstream=newfileinputstream(“D://Workspace\u J/getstr/src/str/activity.text”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null){
//在控制台上打印内容
System.out.println(strLine);
}
String[]tokens=strLine.split(“,”);
for(inta=1;a<3;a++)//a++是编写a=a+1的一种简短方法
{
System.out.println(令牌[Random.nextInt(令牌.长度)]);
}
//关闭输入流
in.close();
}
catch(异常e){//catch异常(如果有)
System.err.println(“错误:+e.getMessage());
}
是否要读取文件中的随机行?请不要使用DataInputStream读取文本,也请将其从示例中删除,并且经常复制此错误想法。抱歉,我添加此内容的地方…您需要将字符串strLine;
移到try
之外,并将代码放入for
循环中。将此放置在何处对于第二个问题,请参见我的编辑。您应该将所有内容都放在try
中以避免任何其他问题(例如,strLine beingnull
)。这一行中的问题System.out.println(tokens[Random.nextInt(tokens.length)];`Cannot对非静态方法nextInt(int)进行静态引用从类型
中,请不要使用DataInputStream阅读文本,也请将其从示例中删除,这一坏主意经常被复制。
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("D://Workspace_J/getstr/src/str/activity.text");
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);
}
String[] tokens = strLine.split(",");
for (int a = 1 ; a < 3 ; a++) //a++ is a short way of writing a = a + 1
{
System.out.println(tokens[Random.nextInt(tokens.length)]);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}