如何从Java文件中读取一定数量的元素?
要从文件中读取固定数量的元素,您可以从文件中读取所需数量的数据元素并进行处理,也可以将整个文件读取到集合或数组中,并为每个n元素进行处理。
示例
按照Java程序,一次读取10个单词的文件内容,并将它们打印在单独的行中。
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingData { public static void main(String args[]) throws FileNotFoundException { //创建文件的对象以读取数据 File file = new File("D://sampleData.txt"); //实例化Scanner类 Scanner sc = new Scanner(file); //一次阅读10个字 while(sc.hasNextLine()) { StringBuffer sb = new StringBuffer(); for(int i=0; i<10; i++) { sb.append(sc.next()+" "); } System.out.println(sb.toString()); } } }
输出结果
nhooo.com originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month. Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning!
示例
以下Java程序将文件的内容读取为字符串,并将其拆分为单词数组,并每10个元素打印该数组的内容。
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadingData { public static void main(String args[]) throws FileNotFoundException { //创建文件的对象以读取数据 File file = new File("D://sampleData.txt"); //实例化Scanner类 Scanner sc = new Scanner(file); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(input+" "); } //将字符串拆分为字符串数组 String str[] = sb.toString().split(" "); //每10个字打印数组的内容 int count = 0; for(int i=0; i< str.length; i++) { count++; System.out.print(str[i]+" "); if(count%10==0) { System.out.println(""); } } } }
输出结果
nhooo.com originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month. Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning!