Studyon Minte9.com
Java SE




Read package file



Add package data/
Add test.txt file (2 lines: Milan, Roma)

package testexample; import java.awt.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import javax.swing.*; public class Testexample extends JFrame { public static void main(String[] args) throws Exception { Testexample frame = new Testexample(); frame.setBounds(200, 200, 300, 200); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } public Testexample() throws Exception { InputStream in = getClass().getResourceAsStream("/data/test.txt"); // src/data/test.txt (will be compiled in .jar) // Contains: Milan, Roma Reader out = new InputStreamReader(in, "utf-8"); BufferedReader reader = new BufferedReader(out); String line = "", output = ""; while( (line = reader.readLine()) != null) { output += line + ", "; } JLabel label = new JLabel(output); // Outputs: Milan, Roma, JPanel panel = new JPanel(); panel.add(label); panel.setForeground(Color.red); add(panel); } }