代码如下,想利用Java中一个读写文件的类,来实现对文件中每一行进行操作创建map,以为利用while循环可以,没想到不能循环,得到的结果只有一个,并不能实现处理每一行,这是为什么呢?求大神指点package testimport java.awt.List;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.LinkedList;import java.util.Map;import java.util.Vector;import w2v.org.nlp.model.W2Vmodel;/** * * @TODO 文件的读写 * @得到句子向量 */public class ReaderAndWriter { String inputpath = ""; String outputpath = ""; @SuppressWarnings("null") public void doit(String inputpath,String outputpath) throws Exception{ BufferedReader input = new BufferedReader(new InputStreamReader( new FileInputStream(new File(inputpath)), "UTF-8")); BufferedWriter output = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputpath), "UTF-8")); String sInput = ""; int count = 0; while ((sInput = input.readLine()) != null) { output.write(sInput); output.newLine(); int length = 100; int abc = 0; int d=0; int w=0; Map<String,float[]> w2vMap = W2Vmodel.word2Vector(outputpath , length, 0); abc=w2vMap.size(); float[][] save=new float[abc][]; for ( Map.Entry<String,float[]>MyWord2Vector: w2vMap.entrySet()){ String word=MyWord2Vector.getKey(); float[ ] Vector=MyWord2Vector.getValue(); save[d]=Vector; w=Vector.length; d++; } int h=d+1; float []sum=new float[w]; for(int i=0;i<save.length;i++){ for(int j=0;j<save[i].length;j++){ //System.out.print(save[i][j]); sum[j]+=save[i][j]; } } for(int j=0;j<w;j++){ System.out.print(sum[j]/h); } System.out.println(); } input.close(); output.flush(); output.close(); } public static void main(String[] args) throws Exception { String inputpath = "C:\\Users\\BG\\Desktop\\result1.txt"; String outputpath = "C:\\Users\\BG\\Desktop\\result2.txt"; ReaderAndWriter n = new ReaderAndWriter(); n.doit( inputpath , outputpath ); } }
添加回答
举报
0/150
提交
取消