import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Read_Write {
public static void main(String[] arg) throws IOException{
String fileName = "data/input/tweet_2012_06";
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
FileWriter fstream = new FileWriter("data/output_tweet_2012_06.txt");
BufferedWriter out = new BufferedWriter(fstream);
int counter = 0;
while ((str = in.readLine())!= null){
System.out.println(counter+" " + str);
out.write(str);
counter++;
}
System.out.println("counter is "+counter);
out.close();
in.close();
}
}
I love tennis and programming. I am not genius, so I collect the knowledge piece by piece.
Showing posts with label text processing. Show all posts
Showing posts with label text processing. Show all posts
Wednesday, May 29, 2013
read and write file in Java
Saturday, March 2, 2013
abnormal characters when using matlab to read txt files
when I read Chinese txt files using fopen function on matlab, many abnormal characters appear while there is no problem using the same code on the other PC. I know it is because of the default system encoding and decoding setting. It should be done if you configure it through control panel.
however, you can also solve the problem from the angle of Matlab, just still use fopen func, only difference is to add one or two parameters which we seldom pay attention to.
You can choose one of the following option for the 4th parameter encoding in above fopen function according to your situation.
however, you can also solve the problem from the angle of Matlab, just still use fopen func, only difference is to add one or two parameters which we seldom pay attention to.
fileID = fopen(filename, permission, machineformat, encoding)
You can choose one of the following option for the 4th parameter encoding in above fopen function according to your situation.
'Big5' 'SO-8859-1' 'windows-932'
'EUC-JP' 'ISO-8859-2' 'windows-936'
'GBK' 'ISO-8859-3' 'windows-949'
'Macintosh' 'ISO-8859-4' 'windows-950'
'Shift_JIS' 'ISO-8859-9' 'windows-1250'
'US-ASCII' 'ISO-8859-13' 'windows-1251'
'UTF-8' 'ISO-8859-15' 'windows-1252'
'windows-1253'
'windows-1254'
'windows-1257'
For my case, I choose the last one, 'UTF-8', and it works!
Thursday, February 28, 2013
Traverse all the files under one folders using matlab and C++ on windows
I usually need to process batches of files in my work. sometimes use matlab, some times use C++:
1. For MATLAB, it is very easy:
2. For C\C++ on windows, it is almost same:
C\C++ on linux seems a little different, i will record that in another post.
1. For MATLAB, it is very easy:
clear;
clc;
close all;
MyDir='D:\Matlab_Code\RA_6\recent-valid\';
% the folder where the targeted files are stored, do not forget the '\' in the end;
AllFileName=dir([MyDir,'*.txt']);
% AllFileName it is a array whose each element is a structure with many attributes
% all the infomation about txt files would be included in AllFilename
% You can even get the numbers of files just by size the AllFileName without traversing all the files
for i=1:length(AllFileName)
Fid=fopen([MyDir,AllFileName(i).name]); % there are many optional parameters you can choose
Content=fgetl(Fid);
while ischar(Content)
Str= Content;
% add your code here
Content=fgetl(Fid);% if you miss this, it could run into a dead loop
end
fclose(Fid);% never forget to close the file
end
2. For C\C++ on windows, it is almost same:
#includeanother method is that you can put all the file names to one txt files using system function, then you can access all the files one by one through reading this txt file.#include #include using namespace std; int main (void) { _finddata_t fileDir; // it is included in io.h, much similar to the AllFileName in the matlab code char* dir="C:\\CZG\\Matlab_Code\\HSS\\*.m"; // notice the end of the above line: // if you write as *.m, then only m file would be found // if you write as *.m*, hen all the extention format start with m could be found, such as .mat, .m file could be found at the same time long lfDir; if((lfDir = _findfirst(dir,&fileDir))==-1l) // in case there is no m file found printf("No file is found\n"); else{ printf("file list:\n"); do{ printf("%s\n",fileDir.name); } while( _findnext( lfDir, &fileDir ) == 0 );// the common way to tranverse the files, and you can not get the number of files without tranversing files, which is different from matlab } _findclose(lfDir); // still do not forget to close unsigned int i=fileDir.size; printf("%d\n",i); return 0; }
#include <iostream>
#include <string>
#include <fstream>
using namesapce std;
int main()
{
system("dir /a-d /b D:\\zhuanfa_singapore\\step_1\\*.* >D:\\zhuanfa_singapore\\step_2\\list.txt");
// all the file names under D:\\zhuanfa_singapore\\step_1 will be stored in
//D:\\zhuanfa_singapore\\step_2\\list.txt
my_in.open("D:\\zhuanfa_singapore\\step_2\\list.txt",ios::in);
string str2="D:\\zhuanfa_singapore\\step_1\\";
while(!my_in.eof())
{
string str1;
getline(my_in,str1);
string str3=str2+str8;
fstream _file;
_file.open(str3.c_str(),ios::in);
if(!_file)
{
cout<<str3<<"not exist!"<<endl;
}
else
{
cout<<str3<<"already exsit!"<<endl;
}
}
my_in.close();
return 0;
}
C\C++ on linux seems a little different, i will record that in another post.
Subscribe to:
Posts (Atom)