我编写了以下代码(可能有更正确的方法,但这不是我的问题):
package activitat2;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
/**
* ACTIVITAT 2 EAC 6
* @author Montse
*/
public class EquipIns{
public static void main(String[] args) {
// TODO code application logic here
EquipIns programa = new EquipIns();
programa.inici();
}
public void inici(){
System.out.println("Institutos....");
System.out.println("1-Registrar los datos de los institutos ");
System.out.println("2-Listar todos los institutos");
System.out.println("3- Salir");
System.out.println("Escoje una opción");
Scanner lector = new Scanner(System.in);
int punts=0;
int enterLlegit =0;
boolean llegit = false;
while(!llegit){
llegit = lector.hasNextInt();
if (llegit){
enterLlegit = lector.nextInt();
switch (enterLlegit) {
case 1:
System.out.println("Registra los datos de los institutos:");
System.out.println("Quantos institutos participan en el concurso?");
boolean leido=false;
int enterLlegit2 =0;
while(!leido){
leido = lector.hasNextInt();
if (leido){
enterLlegit2 = lector.nextInt();
System.out.println("Numero total de centros: "+enterLlegit2);
for (int j = 0; j < enterLlegit2; j++) {
int i=0;
i = enterLlegit2;
if(enterLlegit2>0){
System.out.println("-------------------------------------");
System.out.println("Introduce la info sobre el instituto:");
System.out.println("-------------------------------------");
System.out.println("Id?");
int idInst = lector.nextInt();
System.out.println("Nom Institut:");
String nomInst = lector.next();
System.out.println("Nom Equip:");
String nomEqui = lector.next();
System.out.println();
System.out.println("------------------------------------------");
System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
System.out.println("------------------------------------------");
System.out.println("Id: " +idInst);
System.out.println("Nom Institut: "+nomInst);
System.out.println("Equip: "+nomEqui);
System.out.println("Punts: "+punts);
System.out.println();
}
}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
break;
case 2:
System.out.println("has escogido listar los institutos");
break;
case 3:
System.out.println("Hasta la próxima!");
break;
default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
break;}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
lector.nextLine();
}
}
我需要从这个文件创建一个二进制文件来存储用户输入的数据。
它让我在练习中必须使用void Seek()
. 最大 48 字节(id (4)、Institute (20)、TeamName (20)、Points (4) = 48)。
换句话说,一旦用户输入了数据,就必须将它们记录在文件中。
稍后我必须显示此信息。
编辑 1
我还有一个问题!
我已经完成了同事给我的部分代码,每个屏幕只给我一个结果,因为我想我对用户的每个响应都使用相同的变量。
我把代码:
package activitat2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* ACTIVITAT 2 EAC 6
* @author Montse
*/
public class EquipIns{
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
EquipIns programa = new EquipIns();
programa.inici();
}
public void inici() throws FileNotFoundException, IOException{
RandomAccessFile raf;
raf = new RandomAccessFile("Resultat.bin", "rw");
System.out.println("Institutos....");
System.out.println("1-Registrar los datos de los institutos ");
System.out.println("2-Listar todos los institutos");
System.out.println("3- Salir");
System.out.println("Escoje una opción");
Scanner lector = new Scanner(System.in);
int punts=0;
int enterLlegit =0;
boolean llegit = false;
while(!llegit){
llegit = lector.hasNextInt();
if (llegit){
enterLlegit = lector.nextInt();
switch (enterLlegit) {
case 1:
System.out.println("Registra los datos de los institutos:");
System.out.println("Quantos institutos participan en el concurso?");
boolean leido=false;
int enterLlegit2 =0;
while(!leido){
leido = lector.hasNextInt();
if (leido){
enterLlegit2 = lector.nextInt();
System.out.println("Numero total de centros: "+enterLlegit2);
for (int j = 0; j < enterLlegit2; j++) {
int i=0;
i = enterLlegit2;
if(enterLlegit2>0){
System.out.println("-------------------------------------");
System.out.println("Introduce la info sobre el instituto:");
System.out.println("-------------------------------------");
System.out.println("Id?");
int idInst = lector.nextInt();
System.out.println("Nom Institut:");
String nomInst = lector.next();
System.out.println("Nom Equip:");
String nomEqui = lector.next();
System.out.println();
System.out.println("------------------------------------------");
System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
System.out.println("------------------------------------------");
System.out.println("Id: " +idInst);
System.out.println("Nom Institut: "+nomInst);
System.out.println("Equip: "+nomEqui);
System.out.println("Punts: "+punts);
System.out.println();
raf.seek(0);
raf.writeInt(idInst);
raf.seek(4);
raf.writeUTF(nomInst);
raf.seek(24);
raf.writeUTF(nomEqui);
raf.seek(44);
raf.writeInt(punts);
}
}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
break;
case 2:
System.out.println("has escogido listar los institutos");
raf.seek(0);
System.out.println(raf.readInt());
raf.seek(4);
System.out.println(raf.readUTF());
raf.seek(24);
System.out.println(raf.readUTF());
raf.seek(44);
System.out.println(raf.readInt());
raf.close();
break;
case 3:
System.out.println("Hasta la próxima!");
break;
default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
break;}
}else{
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
lector.nextLine();
}
}
我该如何解决?
我试图更好地解释我认为错误是:
当用户插入团队和机构时,他将其保存在同一个变量中。因此,只要我再次将该数据保存在变量中,它就会删除前一个数据。
我该如何纠正?
能给我举个例子?
这是 Switch Case(选项 1)首先我询问参与的机构数量,然后是 ID,然后是机构名称,然后是团队名称。
在 Switch Case 2 中,文档已准备就绪!有什么方法可以让我在空的时候不会出错?放某种文字说没有机构进入?
谢谢你!!
编辑 2
raf.seek(0);
raf.writeInt(id);
raf.seek(4);
raf.writeUTF(instituto);
raf.seek(24);
raf.writeUTF(equipo);
raf.seek(44);
raf.writeInt(puntos);
// Leer datos
raf.seek(0);
System.out.println(raf.readInt());
raf.seek(4);
System.out.println(raf.readUTF());
raf.seek(24);
System.out.println(raf.readUTF());
raf.seek(44);
System.out.println(raf.readInt());
还有一个问题...
当你把Read UTF放在我身上时,我想UTF是因为它是String数据?
为了使用该方法
seek
,对于这种特殊情况,您需要一个 的实例java.io.RandomAccessFile
。此类用于随机访问文件,以进行读取或写入。该方法
seek
将文件的指针(将从中读取或写入文件)放在由 . 指示的位置pos
。此位置指示要滚动文件的字节数,其中 0 是文件的开头。以下代码举例说明了类
java.io.RandomAccessFile
及其方法的使用seek
:代码:
输出:
更新
如果您计划多次写入同一个 48 字节块,则需要偏移相应的字节数。例如,您可以添加变量
pos
来保存下一个书写位置。即。你可以在阅读中做类似的事情:
writeUTF
readUTF
用于写入或读取字符串。在 Java 中, achar
使用两个字节。所以在 20 个字节中,你最多只能放 10char
秒。UTF 与Unicode和转换格式更密切相关。基本上它是指以位表示字符的方式,这在不同的字符编码标准(UTF-8、ISO-8859-1)之间有很大的不同。