我编译运行这个程序后,写入文件的内容都是乱码,我也不清楚怎么回事,大家帮忙看看,谢谢了程序如下
import java.io.*;
public class Main {
public static void main(String[] args) {
int ch ;
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);
File file1 = new File("D:\\test\\111.txt");
try
{
FileOutputStream fout = new FileOutputStream(file1);
DataOutputStream dout = new DataOutputStream(fout);
System.out.println("输入整数");
int i = Integer.parseInt(bin.readLine());
System.out.println("输入浮点数");
float f = Float.parseFloat(bin.readLine());
System.out.println("输入布尔量");
boolean b = new Boolean(bin.readLine()).booleanValue();
dout.writeInt(i);
dout.writeFloat(f);
dout.writeBoolean(b);
dout.close(); }
catch (FileNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
} }
}
import java.io.*;
public class Main {
public static void main(String[] args) {
int ch ;
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);
File file1 = new File("D:\\test\\111.txt");
try
{
FileOutputStream fout = new FileOutputStream(file1);
DataOutputStream dout = new DataOutputStream(fout);
System.out.println("输入整数");
int i = Integer.parseInt(bin.readLine());
System.out.println("输入浮点数");
float f = Float.parseFloat(bin.readLine());
System.out.println("输入布尔量");
boolean b = new Boolean(bin.readLine()).booleanValue();
dout.writeInt(i);
dout.writeFloat(f);
dout.writeBoolean(b);
dout.close(); }
catch (FileNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
} }
}
你只输入一个int,然后用用ultraedit打开就看到有四个字节(int的长度)了如果想直接在记事本看,就用FileWriter来写,这个是面向字符的
import java.io.*;
import java.io.FileWriter;
public class TestMyString {
public static void main(String[] args) {
int ch ;
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);
File file1 = new File("D:\\111.txt");
try
{
FileWriter dout = new FileWriter(file1);
System.out.println("integer");
String i = bin.readLine();
System.out.println("floate");
String f = bin.readLine();
System.out.println("boolean");
String b =bin.readLine();
dout.write(i);
dout.write(f);
dout.write(b);
dout.close(); }
catch (FileNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
} }
}