Je veux entrer des chaînes à partir d'un fichier et vérifier si chacune des chaînes correspond à une chaîne donnée, mais je n'ai pas pu le faire. Merci d'avance pour votre aide. Mon code est le suivant:entrer une chaîne à partir d'un fichier et comparer la chaîne d'entrée
import java.util.*;
import java.io.*;
import org.apache.commons.io.*;
import java.nio.charset.*;
public class XYZ
{
String s[] = {"Harry","Potter","Pirates","Of","The","Carribean"};
XYZ()
{
save();
String []copyString = load();
for(int i=0; i<6; i++)
{
System.out.print("String " + i + ": " + copyString[i]);
}
}
Ceci est ma fonction save()
pour enregistrer les chaînes dans un fichier:
public void save()
{
DataOutputStream output = null;
try
{
output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("the-file-name.txt")));
for(int i=0; i<6; i++)
{
output.writeUTF(s[i]);
}
output.close();
}
catch(IOException e){}
}
Ceci est ma fonction load()
qui retourne une chaîne:
public String[] load()
{
final Charset UTF_8 = Charset.forName("UTF-8");
String temp;
String copyFromFile[] = new String[6];
DataInputStream input = null;
try
{
input = new DataInputStream(new BufferedInputStream(new FileInputStream("the-file-name.txt")));
for(int i=0; i<6; i++)
{
temp = input.readUTF();
if(temp == "Harry" || temp == "Potter" || temp == "Pirates" || temp == "Of" || temp == "The" || temp == "Carribean")
{
copyFromFile[i] = temp;
}
else
{
System.out.println("Not matched");
}
}
input.close();
}catch (IOException e){}
return copyFromFile;
}
Et enfin, ma fonction main()
:
public static void main(String[]arg)
{
XYZ xyz = new XYZ();
}
}
Sortie:
Not matched
Not matched
Not matched
Not matched
Not matched
Not matched