package com.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; /** * 缓冲读入文件 * @author Administrator * */ public class BufferedInputFile { public static String read(String filename) throws IOException{ //reading input by lines: BufferedReader in = new BufferedReader(new FileReader(filename)); String s; StringBuilder sb= new StringBuilder(); while((s=in.readLine())!=null){ sb.append(s+"\n"); } in.close(); return sb.toString(); } //test1,打开一个文本文件,每次读取一行内容,将每行作为一个string读入,并将该string对象存入linklist中,逆序打印linklist public static void printLinkList(String fileName) throws IOException{ BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt")); String string; LinkedList<String> list = new LinkedList<String>(); while((string=in.readLine())!=null){ list.add(string); } /*for(int i=list.size();i>=0;i--){ System.out.println(list.get(i)); }*/ java.util.ListIterator<String> item = list.listIterator(list.size()); while(item.hasPrevious()){ System.out.println(item.previous()); } while(item.hasNext()){ System.out.println(item.next()); } /** * 结果: * 4-->4-->4:4:4:ghdgml11 3-->3-->3:3:3:flypiger 2-->2-->2:2:2:ghdgml11 1-->1-->1:1:1:flypiger */ } public static void main(String[] args) throws IOException { //System.out.println(read("C:/Users/Administrator/Desktop/exec.txt")); printLinkList("C:/Users/Administrator/Desktop/exec.txt"); } }
不要问为什么,代码诠释一切
时间: 2024-10-11 03:37:46