Чтение из файла - Java (240914)
Формулировка задачи:
Доброго времени суток, в данном коде происходит чтение последней строчки. Как считать допустим 5 последних строчек ?
try
{
FileInputStream fstream = new FileInputStream("baza/help.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine,last;
while (true)
{
if (null != (strLine = br.readLine()))
{
last = strLine;
txt.setText(last);
}
else
{
break;
}
}
}
catch (Exception e)
{
//...
}
}Решение задачи: «Чтение из файла»
textual
Листинг программы
public class TextMaxListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyChar() == KeyEvent.VK_ENTER)
{
try
{
FileWriter sw = new FileWriter("msg/message.txt",true);
String str[] = txtImput.getText().split(System.getProperty("line.separator"));
for(int i=0; i<str.length;i++)
{
sw.write(str[i]+System.getProperty("line.separator")+System.getProperty("line.separator"));
}
sw.close();
txtImput.setText("");
e.consume();
}
catch(Exception e1)
{
//...
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}