Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Scanner;
- public class Fajlovi {
- public int x;
- public static int [][]a= {{1,2,3,4,5},{10,20,30},{70,60,50,40}};
- public void writeIO(String fileName)
- {
- File f= new File(fileName);
- try
- { //povjera nenormalne situacije
- FileWriter fw= new FileWriter(f);
- BufferedWriter bw= new BufferedWriter(fw); //u buffer se slazu podaci koje zelimo upisati, a zatim se jednim potezom kopira na hardDisk
- for(int i=0;i<a.length;i++)
- {
- for (int x:a[i]) //ide za svako x iz datog skupa
- {
- bw.write(x+" ");
- }
- bw.newLine();
- }
- bw.close();
- }
- catch (IOException e) { //hvatanje 'greske' po potrebi
- // TODO Auto-generated catch block
- e.printStackTrace(); //spisak klasa ili/sa greskama
- }
- }
- public void readIO(String fileName) throws IOException
- {
- File f=new File(fileName);
- FileReader fr= new FileReader(f);
- BufferedReader br= new BufferedReader(fr);
- String red= br.readLine();
- while(red!=null)
- {
- System.out.println(red);
- red=br.readLine();
- }
- br.close();
- }
- public void readScanner(String fileName)
- {
- File f=new File(fileName);
- FileReader fr=null;
- Scanner in= null;
- int s=0,x=0;
- try {
- fr = new FileReader(f);
- in = new Scanner(fr);
- while(in.hasNext())
- {
- x=in.nextInt();
- s+=x;
- }
- System.out.println("Suma je "+s);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally
- {
- if(in!=null)
- {
- in.close();
- }
- }
- }
- public void writeNIO(String fileName)
- {
- Path p= Paths.get("C:\\Naziv\\"+fileName);
- try {
- BufferedWriter bw = Files.newBufferedWriter(p);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement