当前位置: 代码迷 >> Java相关 >> 关于文件读写的问题``
  详细解决方案

关于文件读写的问题``

热度:217   发布时间:2006-06-05 22:23:00.0
关于文件读写的问题``
import java.io.*;
public class GreatestCommonDivisor
{
String s2 = new String();
int number1,number2,number3;
int s1,p,r;
int[] array1 = new int[20];
int[] array = new int[20];
public void reader()throws IOException
{
//GreatestCommonDivisor check = new GreatestCommonDivisor();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("MAXnumber.txt")));
s1 = Integer.parseInt(br.readLine());
for(int i = 0; i < s1; i++)
{
s2 = br.readLine();
number3 = s2.indexOf(" ");
number1 = Integer.parseInt(s2.substring(0,number3));
number2 = Integer.parseInt(s2.substring(number3 + 1));
p = number1 * number2;
while(number2 != 0)
{
r = number1 % number2;
number1 = number2;
number2 = r;
}
p = p / number1;
System.out.println("最大公约数是:" + number1);
System.out.println("最小公倍数是:" + p);
array[i] = number1;
array1[i] = p;

}
br.close();
}

public void write()throws IOException
{
BufferedWriter rt=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Answer.txt")));
for(int i = 0;i<s1;i++)
{
rt.write(array[i]);
rt.write(";");
rt.write(array1[i]);
rt.newLine();
}

rt.flush();
rt.close();
System.out.println("写入完毕");
}
public static void main(String args[])throws Exception
{
GreatestCommonDivisor check = new GreatestCommonDivisor();
check.reader();
check.write();
}
}
我上面的程序没有错误可以运行 但是我写入文件的东西为什么是乱码```
搜索更多相关的解决方案: 文件  

----------------解决方案--------------------------------------------------------
你这个程序本身运行就有问题
你应该把("MAXnumber.txt")));的内容写出来,我们才好去调试
----------------解决方案--------------------------------------------------------
问题就在你的MAXnumber.txt中
----------------解决方案--------------------------------------------------------
3
12 32
32 16
12 54
----------------解决方案--------------------------------------------------------
对是我没考虑到` ` 上面的是数据`
----------------解决方案--------------------------------------------------------

没人回答了`?


----------------解决方案--------------------------------------------------------

斑竹 请进啊``


----------------解决方案--------------------------------------------------------
应该不会有很大的问题

----------------解决方案--------------------------------------------------------
为什么会是乱码你应该知道,你写入的是int值,那么它会自动变成相对应的字符
如果你直接把int变成String再写入的话,就会出现数字本身的值了
要不你就用DataOutputStream 的writeInt()的方法写出int值
否则肯定乱码
----------------解决方案--------------------------------------------------------

import java.io.*;
import java.util.*;
class Baoshu{

int m;
String []str;
public void read()throws Exception
{
BufferedReader rd=new BufferedReader(new InputStreamReader(new FileInputStream("Baoshu.txt")));
m=Integer.parseInt(rd.readLine());
str=new String[m];
int i=0;
while(i<m)
{
str[i]=rd.readLine();
i++;
}

}
public void write()throws Exception
{
BufferedWriter rt=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("BaoshuAnswer.txt")));

for(int i=0;i<m;i++)
{
int index=str[i].indexOf(' ');
String s1=str[i].substring(0,index);
String s2=str[i].substring(index+1);
int result=method(Integer.parseInt(s1),Integer.parseInt(s2));
rt.write(result+"");//这里写的也是INT型的啊``
rt.newLine();
}
rt.flush();
rt.close();

}
public int method(int m,int s)
{
Vector vector=new Vector();
for(int i=0;i<m;i++)
vector.addElement(new Integer(i+1));

int c=0;
int i=0;
int size=vector.size();
System.out.println(":::");
while(size>1)
{
i=(i+s-1)%size;
System.out.println(":::"+((Integer)vector.elementAt(i)).intValue());
vector.removeElementAt(i);
size--;
}
return ((Integer)vector.elementAt(0)).intValue();

}
public static void main(String args[])throws Exception
{
Baoshu obj=new Baoshu();

obj.read();
obj.write();

}

}
好象不是这样的 版主 你看看这个
BaoshuAnswer.txt文件中的数据是
3
13 3
5 15
5 2


----------------解决方案--------------------------------------------------------
  相关解决方案