FileInputStream file=new FileInputStream(//这里写文件路径和名字);
BufferedReader reader=new BufferedReader(new InputStreamReader(file));//这里得到流
while((String b=reader.readerLine())!=null){//这里都文件一行一行的都
system.out.priteln(b);//输出到控制台
}
求采纳 纯手打
public static String concatStr() throws IOException {
FileReader fr = null;
BufferedReader reader = null;
fr = new FileReader("F:\\123.txt");
reader = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
system.out.println(line);
}
return sb.toString();
}
程序如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class OpenFile extends JFrame implements ActionListener
{
private JButton openButton; //打开按钮
private JButton exitButton; //退出按钮
private JTextArea textArea; //文本区域
private JScrollPane scrollPane; //带滚动条的面板
private JPanel panel; //普通面板
private JPanel topPanel; //上部面板
public OpenFile()
{
openButton = new JButton("open"); //实例化打开按钮
exitButton = new JButton("exit");//实例化退出按钮
textArea = new JTextArea(15,44);//实例化文本域
panel = new JPanel();//实例化面板
panel.add(openButton);//向面板中添加按钮
panel.add(exitButton);//向面板中添加按钮
scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); //实例化带滚动条的面板
topPanel = new JPanel();//实例化面板
topPanel.add(scrollPane); //向面板中添加带滚动条的面板
setLayout(new BorderLayout(3,3)); //指定布局模式
add(panel,BorderLayout.SOUTH); //将按钮面板添加到窗体南部或叫底部
add(topPanel,BorderLayout.CENTER); //向窗体添加内容面板
setTitle("File Reader"); //设置窗体标题
setBounds(100, 100, 500, 350); //指定窗体显示位置及大小
setResizable(false); //设定窗体大小不可改变
setVisible(true); //设定窗体可见性
openButton.addActionListener(this); //打开按钮添加事件监听
exitButton.addActionListener(this);//退出按钮添加事件监听
}
public static void main(String[] args)
{
new OpenFile();
}
@Override
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser(); //文件选择器
File file = null; //声明文件对象
int result = 0; //文件选择器结果
if(e.getSource() == this.openButton) //如果点击的是打开文件按钮
{
fileChooser.setApproveButtonText("确定"); //设置文件选择窗体按钮文字
fileChooser.setDialogTitle("打开文件"); //设置文件选择窗体标题
result = fileChooser.showOpenDialog(this); //在当前窗体显示
System.out.println(result);
if(result == JFileChooser.APPROVE_OPTION) //如果确定所选择的文件
{
file = fileChooser.getSelectedFile(); //取得所选的文件
try
{
Scanner reader = new Scanner(new FileReader(file)); //将文件放入读取流
reader.useDelimiter("\r\n"); //以回车换行作为读取分隔符
while(reader.hasNext()) //如果还是内容
{
String content = reader.next(); //当前读取的内容
System.out.println(content); //将内容打印到控制台
this.textArea.setText(this.textArea.getText() + content); //将内容显示到文本域
}
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
if(e.getSource() == this.exitButton)
{
System.exit(1); //退出窗体
}
}
}
有问题欢迎提问,满意请采纳!