使用io包中的类实现该功能:把一个文件中的内容复制到另一个文件中。
FileReader BufferedReader
FileWriter BufferedWriter
String StringBuffer
随机生成10个点坐标,将它们的坐标值以二进制的形式写入d:\test_10.dat中,然后可以用ultraedit打开该文件查看内容。
提示:
1. 创建类Point,定义点坐标(x, y),同时利用构造方法生成点对象,坐标值为随机值。
2. 以二进制的形式写入某文件中,使用io包中的FileOutputStream和DataOutputStream。
这两个怎么写..我对这部分不是很熟悉..谢谢了```
FileReader BufferedReader
FileWriter BufferedWriter
String StringBuffer
随机生成10个点坐标,将它们的坐标值以二进制的形式写入d:\test_10.dat中,然后可以用ultraedit打开该文件查看内容。
提示:
1. 创建类Point,定义点坐标(x, y),同时利用构造方法生成点对象,坐标值为随机值。
2. 以二进制的形式写入某文件中,使用io包中的FileOutputStream和DataOutputStream。
这两个怎么写..我对这部分不是很熟悉..谢谢了```
解决方案 »
- 调试一个程序的问题
- 做了一个小游戏,eclipse或者JAR比java慢 为什么?
- java 自定义类运行报错
- 请问:在java中如何将Object类型的数转化为int型呢?非常感谢各位的指点
- 为什么去掉//cir.showValue();这一行编译通不过呢
- 请教各位高手
- 页面中的applet,每当页面刷新时,applet中的一行小图标就右移。但用Ctrl+F5刷新页面,该applet就能保持初始状态,请问是什么原因?
- 如何将一字符串分割成一数组,注意此字符串的分割符是由另一字符串组成而非单一的字符?
- help
- 在uml中“依赖”与“关联”到底有什么区别?
- 高分求用Java怎么作工具栏?解决后再加分!
- 一个外部面板用来响应鼠标消息,在其中内置两个面板对应两套坐标显示图像,请问大家如何解决内部两个面板的大小设置问题?
--------------------------------------
/**
* project_name: Test
* package_name: csdn_Test_20071126
* package_declaration: package csdn_Test_20071126;
* filename: RemoveFileTest.java
* author: yuhaiming
* date: 2007-12-4
*/
package csdn_Test_20071126;
import java.io.*;
public class RemoveFileTest {
/**
* 主函数处理
*/
public static void disposal(){
File oldFile = new File("test.txt");
System.out.println(oldFile.getAbsolutePath());
File newFile = new File("testResult.txt");
oldFile.renameTo(newFile);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
disposal();
}}
------------------/**
* project_name: Test
* package_name: csdn_Test_20071126
* package_declaration: package csdn_Test_20071126;
* filename: RemoveFileTest.java
* author: yuhaiming
* date: 2007-12-4
*/
package csdn_Test_20071126;
import java.io.*;
public class RemoveFileTest {
/**
* 主函数处理
*/
public static void disposal(){
try{
File oldFile = new File("test.txt");
File newFile = new File("testResult.txt");
BufferedReader reader = new BufferedReader(new FileReader(oldFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
String read_line = reader.readLine();
StringBuffer buffer = new StringBuffer(read_line);
System.out.println(read_line);
while(read_line!=null){
buffer.append(read_line+"\n");
read_line = reader.readLine();
}
writer.write(buffer.toString());
reader.close();
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
disposal();
}}
我在text.txt里面有三行内容,执行完程序后
testResult.txt里面内容变成了一行.
import java.io.*;
import java.util.Random;class Points //生成坐标类,这里生成的是1024*768显示器上的坐标
{
private int x,y;
private String tempoint,tempx,tempy;
private Random rpoint=new Random();
public Points()
{
this.x=rpoint.nextInt(1025);
this.y=rpoint.nextInt(769);
tempx=String.valueOf(x);
tempy=String.valueOf(y);
this.tempoint="("+tempx+","+tempy+")";
}
public String getPoint()
{
return tempoint;
}
}public class t //主类
{
public static void main(String[] args)
{
try{
Points[] po={
new Points(),
new Points(),
new Points(),
new Points(),
new Points(),
new Points(),
new Points(),
new Points(),
new Points(),
new Points()
};
File file=new File("d:\\test_10.dat");
FileWriter fw=new FileWriter(file);
for(int i=1;i<=10;i++)
{
fw.write("第"+String.valueOf(i)+"点坐标是:"+po[i-1].getPoint()+"\r\n");
}
fw.close();
}catch(java.io.IOException e){System.out.println (e);}
}
}
buffer.append(read_line+"\n");
中的“\n”修改为:
buffer.append(read_line+System.getProperty("line.separator"));这样就实现了不同平台的换行了
try {
Random ram = new Random();
int x = 0;
int y = 0;
FileOutputStream fos = new FileOutputStream("c:\\test_10.dat");
DataOutputStream dos = new DataOutputStream(fos);
for (int i = 0; i < 10; i++) {
x = ram.nextInt();
y = ram.nextInt();
Point pos = new Point(x, y);
String posX = Integer.toBinaryString(pos.x);
String posY = Integer.toBinaryString(pos.y);
dos.writeBytes("(" + posX + "," + posY + ")\n");
}
dos.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}