long型和double型数 怎么写进文档 和输出啊 顺便帮看看 我那char型和string型 做法对不
import java.util.Scanner;
import java.io.*;
public class basicType {
public static void main(String[] args){
int a=1;
long b=2;
double d=8.0;
char c='a';
String str="hello";
try{
File inputFile = new File("D:\\JAVA\\test.txt");
File outputFile = new File("D:\\JAVA\\test.txt");
FileReader in=new FileReader(inputFile);
FileWriter out=new FileWriter(outputFile);
out.write(a);
//out.write(b);
//out.write(d);
out.write(c);
out.write(str);
out.close(); //关闭此流,但先刷新它
System.out.println("已将信息写入test.txt");
System.out.println("整型数值为"+in.read());
//System.out.println("长整型数值为"+);
//System.out.println("double型数值为"+);
System.out.println("char为"+(char)in.read());
System.out.print("字符串为");
while(in.ready()) //
{
System.out.print((char)in.read());
}
}catch(
IOException e){
System.err.println(e);
}
}
}
long型和double型数 怎么写进文档 和输出啊 顺便帮看看 我那char型和string型 做法对不
------解决方案--------------------DataOutputStream这个可以写入基本数据类型
------解决方案--------------------如果想要用程序读写的话,需要用过滤流DataInputStream和DataOutputStream完成,下面是代码实现
Java code
import java.io.*;
public class BasicType {
public static void main(String[] args){
int a=1;
long b=2;
double d=8.0;
char c='a';
String str="hello";
try{
File outputFile = new File("test.txt");
FileOutputStream out=new FileOutputStream(outputFile);
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.write(a);
dataOut.writeLong(b);
dataOut.writeDouble(d);
dataOut.write(c);
dataOut.writeUTF(str);
dataOut.close(); //关闭此流,但先刷新它
System.out.println("已将信息写入test.txt");
File inputFile = new File("test.txt");
DataInputStream in=new DataInputStream(new FileInputStream(inputFile));
System.out.println("整型数值为"+in.read());
System.out.println("长整型数值为"+in.readLong());
System.out.println("double型数值为"+in.readDouble());
System.out.println("char为"+(char)in.read());
System.out.print("字符串为"+in.readUTF());
in.close();
}catch(IOException e){
System.err.println(e);
}
}
}