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.
Created
September 28, 2023 11:50
-
-
Save nherbaut/6c1663c3cbf2f6e9469529980d182ccb to your computer and use it in GitHub Desktop.
L2.2.2 Les boucles For
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
execute me on https://java.miage.dev/?gistId=6c1663c3cbf2f6e9469529980d182ccb