Java - Guardar valores de una Matriz en un archivo

 
Vista:

Guardar valores de una Matriz en un archivo

Publicado por Enrique (1 intervención) el 15/12/2016 06:21:11
Es un ejercicio de mi clase y quieren que guarde los valores de una nueva matriz en un archivo ya tengo el archivo pero no se como meter los valores
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package matriz;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import java.util.Scanner;
import java.util.StringTokenizer;
 
/**
 *
 * @author RALF
 */
public class Matriz {
 
    int ren, col;
    int [][] Dat;
 
    public Matriz(){
        ren = 0;
        col = 0;
    }
 
    public Matriz(int n){
        ren = n;
        col = n;
 
        Dat = new int[ren][col];
    }
 
    public Matriz(int r, int c){
        ren = r;
        col = c;
 
        Dat = new int[ren][col];
    }
 
    public void LeerConsola(){
        int i,j;
        Scanner consola = new Scanner(System.in);
 
        for(i=0; i < ren; i++){
            for(j=0; j < col; j++){
                System.out.print("Leer [" + i + "," + j + "] ");
                Dat[i][j] = consola.nextInt();
                System.out.println(" ");
            }
        }
    }
 
    public void Imprimir(){
        int i,j;
        for (i=0; i < ren; i++){
            for(j=0; j < col; j++){
                System.out.print(Dat[i][j]);
                System.out.print(", ");
            }
            System.out.println(" ");
        }
    }
 
    public Matriz Suma(Matriz m){
        Matriz temp;
        temp = new Matriz(ren, col);
 
        int i,j;
        for (i=0; i < ren; i++){
            for(j=0; j < col; j++){
                temp.Dat[i][j] = Dat[i][j] + m.Dat[i][j];
            }
        }
        return temp;
    }
    public Matriz Multiplica(Matriz m){
        Matriz temp;
        temp = new Matriz(ren, col);
 
        int i,j;
        for (i=0; i < ren; i++){
            for(j=0; j < col; j++){
                temp.Dat[i][j] = Dat[i][j] * m.Dat[i][j];
            }
        }
        return temp;
    }
 
    public void LeerArchivo(String f) throws FileNotFoundException, IOException  {
        String line;
        int flag = 1;
        int val = 0;
 
        try (
            InputStream fis = new FileInputStream(f);
            InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
            BufferedReader br = new BufferedReader(isr);
        )
        {
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
 
		StringTokenizer st = new StringTokenizer(line,",");
		while (st.hasMoreElements()) {
			System.out.println(st.nextElement());
                        if(flag==1){
                            ren = Integer.parseInt((String)st.nextElement());
                            flag=2;
 
                }
            }
        }
        }
    }
 
    public void GuardarArchivo(String f) {
        FileWriter archivo1 = null;
        PrintWriter pw = null;
 
        try{
 
            archivo1 = new FileWriter("Matriz1.txt",true);
             pw = new PrintWriter(archivo1);
                pw.println();//aqui agregas la cadena que guardas con un sout y un Scanner
 
        }catch(IOException e){
            e.printStackTrace();
        }finally{
 
            try {
                 if(archivo1!=null)
                    archivo1.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
 
        }
    }
 
 
    /**
     * @param args the command line arguments
//     */
    public static void main(String[] args) throws IOException   {
        Matriz a,b,c,  x,y,z; 
        
        x = new Matriz();
        
      x.LeerArchivo("Matriz1.dat");
      x.LeerArchivo("Matriz2.dat");
      x.GuardarArchivo("Matriz2.dat");
//        a = new Matriz(2,2);
//        b = new Matriz(2,2);
//        a.LeerConsola();
//        b.LeerConsola(); 
//        c = a.Multiplica(b);
//        c.Imprimir();




    }



}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder