Skip to content

Instantly share code, notes, and snippets.

@nherbaut
Created September 28, 2023 11:50
Show Gist options
  • Select an option

  • Save nherbaut/6c1663c3cbf2f6e9469529980d182ccb to your computer and use it in GitHub Desktop.

Select an option

Save nherbaut/6c1663c3cbf2f6e9469529980d182ccb to your computer and use it in GitHub Desktop.
L2.2.2 Les boucles For

Observez la boucle for ci-dessus. Nous pouvons utiliser ce type de boucle for, car nous souhaitons accéder, dans l'ordre, à chacun des éléménts du fichier.

import java.util.Map;
import java.util.HashMap;
public class HelloWorld
{
public void runHello(){
FileResource f = new FileResource("hello_unicode.txt");
for(String line : f.lines()){
System.out.println(line);
}
}
public static void main(String args[]){
HelloWorld hw = new HelloWorld();
hw.runHello();
}
class FileResource{
private final static Map<String,String> content;
static {
content=new HashMap<>();
content.put("hello_unicode.txt","1,2\n4,6\n7,9");
}
private final String resource;
public FileResource(String ref){
resource=content.get(ref);
}
public String[] lines(){
return resource.split("\n");
}
}
}
@nherbaut
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment