Java文件I / O:为什么我总是得到I / O异常?(Java File I/O: Why do I always get an I/O exception?)

我试图写入一个文件,然后从同一个文件中读取。 输出为“错误:I / O异常”。 意味着程序正在捕获IOException。

public class fileIO {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            File file = new File("io.txt");
            BufferedReader read = new BufferedReader(new FileReader(file));
            BufferedWriter write = new BufferedWriter(new FileWriter(file));

            String needs = "This is going to the file";
            write.write(needs);

            String  stuff = read.readLine();
            while(stuff != null)
            {
                System.out.println(stuff);
                stuff = read.readLine();
            }

        }
        catch(IOException e)
        {
            System.out.println("Error: I/O Exception");
        }
        catch(NullPointerException e)
        {
            System.out.println("Error: NullPointerException");
        }
    }
}'

I am trying to write to a file and then read from that same file. The output is "Error: I/O exception". Meaning that the program is catching the IOException.

public class fileIO {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            File file = new File("io.txt");
            BufferedReader read = new BufferedReader(new FileReader(file));
            BufferedWriter write = new BufferedWriter(new FileWriter(file));

            String needs = "This is going to the file";
            write.write(needs);

            String  stuff = read.readLine();
            while(stuff != null)
            {
                System.out.println(stuff);
                stuff = read.readLine();
            }

        }
        catch(IOException e)
        {
            System.out.println("Error: I/O Exception");
        }
        catch(NullPointerException e)
        {
            System.out.println("Error: NullPointerException");
        }
    }
}'

原文:https://stackoverflow.com/questions/27028559
2023-11-20 12:11

满意答案

为了顺利完成,你应该使用一个Timer 。 假设帧速率是24 fps,则意味着每1/24 = 0.0416秒或大约42毫秒是一帧。 因此,如果您的计时器每42ms计时一次,您可以向后移动mePlayer.Position

XAML:

<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4"
              LoadedBehavior="Manual" ScrubbingEnabled="True"/>

码:

    System.Windows.Threading.DispatcherTimer dispatcherTimer;
    int t = 240000; // 4 minutes = 240,000 milliseconds

    public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        //tick every 42 millisecond = tick 24 times in one second
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42);

    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        // Go back 1 frame every 42 milliseconds (or 24 fps)
        t = t - 42;
        mePlayer.Position = TimeSpan.FromMilliseconds(t);
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        mePlayer.Play();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Pause and go to 4th minute of the video then start playing backward
        mePlayer.Pause();                               
        mePlayer.Position = TimeSpan.FromMinutes(4);
        dispatcherTimer.Start();
    }

To do it smoothly you should use a Timer. Assuming the frame rate is 24 fps, then it means that is one frame every 1/24 = 0.0416 second or approximately 42 millisecond. So if your timer ticks every 42ms you can move mePlayer.Position backward:

XAML:

<MediaElement x:Name="mePlayer" Source="C:\Sample.mp4"
              LoadedBehavior="Manual" ScrubbingEnabled="True"/>

Code:

    System.Windows.Threading.DispatcherTimer dispatcherTimer;
    int t = 240000; // 4 minutes = 240,000 milliseconds

    public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        //tick every 42 millisecond = tick 24 times in one second
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 42);

    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        // Go back 1 frame every 42 milliseconds (or 24 fps)
        t = t - 42;
        mePlayer.Position = TimeSpan.FromMilliseconds(t);
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        mePlayer.Play();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Pause and go to 4th minute of the video then start playing backward
        mePlayer.Pause();                               
        mePlayer.Position = TimeSpan.FromMinutes(4);
        dispatcherTimer.Start();
    }

相关问答

更多

如何在WPF中播放声音?(How To Play Sound In WPF?)

您可以使用WPF的MediaElement。 有关更多信息,请访问官方文档: https : //msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(v = vs.100).aspx You can use WPF's MediaElement for this. For more information, visit the official documentation: https://msdn.micro...

如何在WPF中向后播放视频?(How to play video backwards in WPF?)

为了顺利完成,你应该使用一个Timer 。 假设帧速率是24 fps,则意味着每1/24 = 0.0416秒或大约42毫秒是一帧。 因此,如果您的计时器每42ms计时一次,您可以向后移动mePlayer.Position : XAML: <MediaElement x:Name="mePlayer" Source="C:\Sample.mp4" LoadedBehavior="Manual" ScrubbingEnabled="True"/> 码: Syste...

视频资源无法在wpf mediaelement中播放(video resource won't play in wpf mediaelement)

您不能将视频作为资源嵌入到程序集中,而是从MediaElement中播放它。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/150cb4e7-9107-4d07-8c81-6c2ef9734529/how-to-play-a-wmv-file-as-a-embed-resource-在-resourceresx与- MediaElement的-而不是内容?论坛= WPF 您可能能够做的最接近的事情是将文件添加为content并从文件系...

如何向后播放音频?(How to play audio backwards?)

通常,在使用ASBD时,这些字段描述了由此描述表示的缓冲区中的样本数据的完整布局 - 其中通常这些缓冲区由AudioBufferList中包含的AudioBuffer表示。 但是,当ASBD具有kAudioFormatFlagIsNonInterleaved标志时,AudioBufferList具有不同的结构和语义。 在这种情况下,ASBD字段将描述列表中包含的一个AudioBuffers的格式,并且列表中的每个AudioBuffer被确定为具有单个(单声道)音频数据通道。 然后,ASBD的mCh...

WPF:为什么MediaElement不播放?(WPF: Why MediaElement does not play?)

媒体源应该来自您的文件系统示例 mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv") the media source should be from your file system example mediaElement.Source = new Uri(@"C:\User\Admin\Images\Wildlife.wmv")

WPF中的视频缩略图(Video thumbnails in WPF)

您可以通过将MediaPlayer的ScrubbingEnabled属性设置为true来实现此目的,并且您仍需要继续前进以实时捕获任何电影。 有关更多样本,请阅读Clint Rutkas撰写的这篇精彩博客文章: http://www.betterthaneveryone.com/archive/2009/10/02/882.aspx You do this by setting the MediaPlayer's property of ScrubbingEnabled to true and s...

WPF媒体元素视频源(WPF Media Element Video Source)

这对我有用。 添加LoadedBehavior =“手动” <MediaElement LoadedBehavior="Manual" x:Name="bgvideo" Width="800" Height="600" Source="Videos/BG_LOOP_BIG.wmv" /> 然后在后面的代码中你需要播放媒体 bgvideo.Play() 你还需要丢掉uri中的第一个'/'。 心连心 This works for me. Add LoadedBehavior="Manual" <M...

在WPF应用程序中嵌入flv(flash)播放器(Embedding flv (flash) player in WPF app)

如果embed代码包含iframe ,请使用iframe的src并将其放入WebBrowser源代码中。 例如,对于Youtube视频,这是嵌入代码: <iframe width="560" height="315" src="http://www.youtube.com/embed/[youtubeVideoId]" frameborder="0" allowfullscreen> </iframe> 您将在WPF WebBrowser中使用此URL: http://www.youtu...

向后播放视频(Play video backwards)

问题在于输出URL,这是正确的方法 import UIKit import AVFoundation import AVKit class ViewController: UIViewController { var playerViewController = AVPlayerViewController() var playerView = AVPlayer() override func viewDidAppear(_ animated: Bool) { ...

在WPF中集成Vimeo的视频(Integration of Vimeo's video in WPF)

WPF WebBrowser控件使用其窗口中嵌入的Internet Explorer ActiveX控件。 因此,它与安装的Internet Explorer版本具有相同的限制。 默认情况下,它将使用IE 7兼容性,因此更现代的JavaScript可能无法正常工作。 您可以通过在HTML中使用固定的DOCTYPE (但显然,您不控制Vimeo的HTML)或通过设置注册表键在计算机上启动可执行文件来强制控件使用更现代的渲染引擎在这篇微软文章中: 浏览器仿真 您需要为.exe名称添加一个条目。 例如,...

相关文章

更多

Hadoop的I/O

1. 数据完整性:任何语言对IO的操作都要保持其数据的完整性。Hadoop当然希望数据在存储和处理中不 ...

Hadoop I/O系统介绍

看过很多Hadoop介绍或者是学习的帖子和文章,发现介绍Hadoop I/O系统的很少。很多文章都会介 ...

Hadoop1.0.4 HDFS I/O性能测试

分布式RDF查询引擎的项目需要在节点之间传输中间结果,考虑HDFS可以作为一个备选的传输媒介,于是对H ...

Java中如何用I/O流读取一个Word文档的内容?

BufferedReader br = new BufferedReader(new FileRead ...

I18n的一个问题

升级了,2.2.2, 用了I18n. 问题来了。 以前model validation 出错的默认消 ...

I18N 国际化 简介

软件的国际化: 软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提 ...

《Java 2 图形设计卷I:AWT》(Graphic Java 1.2 Mastering the JFC Volume I:AWT )扫描版[PDF]

中文名: Java 2 图形设计卷I:AWT 原名: Graphic Java 1.2 Mast ...

Java 流(Stream)、文件(File)和IO

Java 流(Stream)、文件(File)和IO Java.io包几乎包含了所有操作输入、输 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本