//DelimitedDataFile.java bean文件源代码
//导入java包
import java.io.*;
import java.util.StringTokenizer;public class DelimitedDataFile
{private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//设置文件路径
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
}
//得到文件路径
public String getPath() {
return path;
}
//关闭文件
public void fileClose() throws IOException
{
file.close();
}
//读取下一行记录,若没有则返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}
if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
} //以字符串的形式返回整个记录
public String returnRecord()
{return currentRecord;
}
}
//导入java包
import java.io.*;
import java.util.StringTokenizer;public class DelimitedDataFile
{private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//设置文件路径
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
}
//得到文件路径
public String getPath() {
return path;
}
//关闭文件
public void fileClose() throws IOException
{
file.close();
}
//读取下一行记录,若没有则返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}
if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
} //以字符串的形式返回整个记录
public String returnRecord()
{return currentRecord;
}
}
Read.jsp<html>
<head>
<title>读取一个文件</title>
</head>
<body bgcolor="#000000">
<%--调用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean><h3>文件内容:</h3><p><% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<% out.print(count); %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
<%
String fileName = request.getRealPath(".") + "test.txt";
printFile(fileName,"欢迎大家");
%><%!
void printFile(String fileName, String message){
PrintWriter log;
try {
log = new PrintWriter(new FileWriter(fileName, true), true);
log.print(message);
//将上句 print 改为 println 则自动换行
log.close();
}catch (IOException e){
System.err.println("无法打开文件: " + fileName);
}
}
%>
public class Text{
public static void main(String [] args){
String arg1 = args[0];//一段字符
String arg2 = args[1];//文件名
try{
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(arg1)));
out.print(arg2);
out.close();
}
catch(IOException i){
System.out.print("Wrong.");
}
}
}
String arg1 = args[0];//文件名
String arg2 = args[1];//一段字符
* Created on 2005/01/31
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package socket;import java.io.FileWriter;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
* @author sk
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test extends JFrame { private javax.swing.JPanel jContentPane = null;
private JButton jButton = null;
private JTextField jTextField = null;
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(83, 116, 120, 32);
jButton.setText("Line in");
jButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
FileWriter fwriter;
String s = jTextField.getText().toString();
try {
fwriter = new FileWriter("text.txt",true);
fwriter.write(s+"\n\r");
fwriter.close();
}catch (IOException ex) {
System.out.println(ex);
}
}
});
}
return jButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(34, 20, 214, 53);
}
return jTextField;
}
public static void main(String[] args) {
Test text = new Test();
text.show();
}
/**
* This is the default constructor
*/
public Test() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextField(), null);
}
return jContentPane;
}
}
String s="要输出的字符串";
ps.println(s);
直接用fileoutputstream也行。String s="要输出的字符串";
byte[] b=s.getBytes();
FileOutputStream fos=new FileOutputStream("D:/out.txt");
fos.write(b);友情提示一下,我还没捕抓异常。。
FileOutputStream fos = new FileOutputStream("c:\\c.txt");
fos.write(s.getBytes("ISO-8859-1"));
fos.flush();
fos.close();