如何使用缓冲的Reader或Scanner从java中的文本文件中读取前5行?这是我的代码
final int assumedLineLength = 16;
File file = new File("src/hw7p1/Acronyms.txt");
HashMap<String, String> hashMap = new HashMap<String, String>((int)(file.length() / assumedLineLength) * 2);
BufferedReader reader = null;
int linecount = 0 ;
String eachLine = null;
try {
reader = new BufferedReader(new FileReader(file));
for ( eachLine = reader.readLine();
eachLine != null;
eachLine = reader.readLine()) {
hashMap.put(eachLine, " ");
linecount++; int i = 0;
}
TreeMap<String, String> sorted = new TreeMap<>(hashMap);
Set<Entry<String, String>> sortings = sorted.entrySet();
for(Entry<String, String> sort : sortings){
System.out.println(sort.getKey() + " " + sort.getValue());
}
}catch (IOException e) {
e.printStackTrace();
}
} 它打印出文本文件,但我想要做的只是让它打印出前5行。任何帮助,将不胜感激。
答案 0 :(得分:1)
在for循环中,如果只需要5行,请不要使用eachLine。使用
import UIKit
class ContactosViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let defaultValues = UserDefaults.standard
var contactos: String = ""
var numerosmov: String = ""
var tablacontacto = [String] ()
var tablanumero = [String] ()
let cellIdentifier: String = "cell"
let cellIdentifier2: String = "cell2"
@IBOutlet weak var contactoTable: UITableView!
@IBOutlet weak var numTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
// Do any additional setup after loading the view.
datosRecividos(contactos, numerosmov)
contactoTable.delegate = self
numTable.delegate = self
contactoTable.dataSource = self
numTable.dataSource = self
contactoTable!.reloadData()
numTable!.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
contactoTable.reloadData()
numTable.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func datosRecividos(_ contactosr: String, _ numerosr: String)
{
tablacontacto.append(contactosr)
tablanumero.append(numerosr)
let usercontacto = defaultValues.array(forKey: "contactoTable")
let usernumero = defaultValues.array(forKey: "numeroTable")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (tableView.tag == 1)
{
return(tablacontacto.count)
}
else if (tableView.tag == 2)
{
return(tablanumero.count)
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
if (tableView.tag == 1)
{
cell.textLabel?.text = tablacontacto[indexPath.row] as! String
}
else if (tableView.tag == 2)
{
cell.textLabel?.text = tablanumero[indexPath.row] as! String
}
return(cell)
}
}
答案 1 :(得分:0)
public String[] readLines(int noLines, String path) throws IOException {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String[] textData = new String[noLines];
int i;
for (i=0; i < noLines; i++) {
textData[i] = br.readLine();
}
br.close( );
return textData;
}