可以使用RandomAccessFile类。例如要从100字节开始输出工作目录下的datatxt文件的类容。
package konwtest1;
import javaioFileNotFoundException;
import javaioIOException;
import javaioRandomAccessFile;
public class Test1
{
public static void main(String[] args)
{
long pos = 100;
try
{
String str = "";
RandomAccessFile randomAccessFile = new RandomAccessFile("datatxt", "rw");
randomAccessFileseek(pos);//将文件流的位置移动到pos字节处
while( (str = randomAccessFilereadLine()) != null)
{
Systemoutprintln(str);
}
randomAccessFileclose();
} catch (FileNotFoundException e)
{
eprintStackTrace();
} catch (IOException e)
{
eprintStackTrace();
}
}
}
VB中的len()函数返回的就是字符的个数,可以用顺序型打开文件,然后读一遍,用len()累计算出字符个数。这个方法能返回正确值的前提是文件里面没乱码。对于ANSI格式的TXT能直接读,Unicode及其他编码的要掐掉文件头再算。TXT以外的格式小生就无能为力了。⊙﹏⊙b
import javaioFile;
import javaioRandomAccessFile;
/
2016年8月31日下午7:00:37
@author 3306 TODO 计算字节数
/
public class FileUtil {
public static void main(String[] args) {
String filePath = "d:/testtxt";// d盘必须存在testtxt文件
readEachLine(filePath);
}
/
打印文件每一行的字节数
@param filePath
文件路径
/
private static void readEachLine(String filePath) {
try {
File file = new File(filePath);
if (fileexists()) {// 文件存在
RandomAccessFile accessFile = new RandomAccessFile(file, "r");// 只赋予读的权限
String line = "";
long lineIndex = 1;
while (null != (line = accessFilereadLine())) {
Systemoutprintln("line" + (lineIndex++) + ": " + linegetBytes()length);// 打印行号和字节数
}
accessFileclose();
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
比如处理txt文件
@echo offcd/d "%~dp0"
for /f "delims=" %%i in ('dir/b/s txt')do ren "%%i" "%%~ni%%~zi%%~xi"
// 查看copy例子即可。
import javaawtBorderLayout;
import javaawtCursor;
import javaawtToolkit;
import javaawteventActionEvent;
import javaawteventActionListener;
import javabeansPropertyChangeEvent;
import javabeansPropertyChangeListener;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioInputStream;
import javaioOutputStream;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJProgressBar;
import javaxswingSwingWorker;
public class Bar extends JFrame implements PropertyChangeListener
{
private static final long serialVersionUID = 1L;
private static final int MIN = 0;
private static final int MAX = 100;
private JProgressBar progressBar;
private JButton copy;
private Task task;
public Bar(String title)
{
setTitle(title);
setLayout(new BorderLayout());
progressBar = new JProgressBar(MIN, MAX);
progressBarsetValue(MIN);
progressBarsetStringPainted(true);
add(progressBar, BorderLayoutCENTER);
copy = new JButton("拷贝");
copyaddActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
start();
copysetEnabled(false);
}
});
add(copy, BorderLayoutEAST);
}
class Task extends SwingWorker<Void, Void>
{
@Override
public Void doInBackground()
{
setProgress(MIN);
File file = new File("ajpg");
long size = filelength();
byte[] bts = new byte[(int) (size / (Mathrandom() 50 + MAX - 50))];
int length = 0;
long cLength = 0;
try
{
InputStream stream = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("copy_aajpg"));
while((length = streamread(bts)) > 0)
{
oswrite(bts, 0, length);
cLength += length;
setProgress((int) (cLength MAX / size));
Threadsleep(30);
}
osflush();
osclose();
streamclose();
}
catch(Exception e)
{
eprintStackTrace();
}
return null;
}
@Override
public void done()
{
setTitle("拷贝完毕");
ToolkitgetDefaultToolkit()beep();
copysetEnabled(true);
setCursor(null);
}
}
public void propertyChange(PropertyChangeEvent evt)
{
if("progress" == evtgetPropertyName())
{
int progress = (Integer) evtgetNewValue();
progressBarsetValue(progress);
}
}
private void start()
{
task = new Task();
taskaddPropertyChangeListener(this);
taskexecute();
setTitle("正在拷贝");
setCursor(CursorgetPredefinedCursor(CursorWAIT_CURSOR));
}
private static void createAndShowGUI()
{
Bar frame = new Bar("Bar");
framesetAlwaysOnTop(true);
framesetSize(280, 60);
framesetResizable(false);
framesetLocationRelativeTo(null);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
public static void main(String[] args)
{
javaxswingSwingUtilitiesinvokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
话不多说先上代码
Scanner sc = new Scanner(f);
int i = 0, j=0, k=0;
while(schasNext()) {
scnext();
i++;
}
sc = new Scanner(f);
while(schasNextLine()) {
scnextLine();
j++;
}
sc = new Scanner(f);
while(schasNextByte()) {
scnextByte();
k++;
}
Systemoutprintln(i+" "+j+" "+k);
然后区分一下字节和字符的概念,一个字节是一个8位的2进制数,一个字符是'a'、'1'这种
楼主是不是说的统计字符呢
scanner类中并没有统计字符的方法 用filereader貌似方便一点
这是只是提供一个思路,多查api文档,多用google,这类问题都不叫问题。
搜到这段代码后,我表示很惭愧
public class Test {
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(Systemin);
Systemoutprintln("请输入路径");
String path=inputnext();
int charNum= 0 ;
int wordsNum= 0;
int lineNum = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr);
while( brread()!= -1){
String s = brreadLine();
charNum+=slength();
wordsNum +=ssplit(" ")length;
lineNum ++;
}
isrclose();//关闭
Systemoutprintln("字符数:"+charNum+"\t单词数:"+wordsNum+"行 数:"+lineNum);
}
}
以上就是关于用java如何读取一个文件的指定字节位置的数据全部的内容,包括:用java如何读取一个文件的指定字节位置的数据、VB中的LOF函数返回的是字节数。有何方法可以获得一个文件的字符数吗、java读取txt文件每一行多少个字节等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)