首页 \ 问答 \ Scala和案例类(Scala and case classes)

Scala和案例类(Scala and case classes)

我正在阅读文件,并将行号附加到每个文件。 下面是一个列表来帮助这个例子:

val testList: List[String] = List("Dont", "Do", "It"

val output: List[(String, Int)] = (testList.zipWithIndex)

我的程序使用._1&._2访问器获得了一点代码嗅觉。 我创建:

case class File(line: String, lineNum: Int)

但是,我知道如何最好地利用这个案例类的唯一方法是使用以下内容:

val fileOutput: List[File] = for{(line, lineNum) <- output} yield{File(line, lineNum)}

我的问题:为什么我不能这样做?

val output: List[File] = (testList.zipWithIndex)

为了使用案例课程,我对我的文件做了两个通行证感到厌倦。

提前致谢


I am reading in a file, and appending line numbers to each file. Below is a List to facilitate this example:

val testList: List[String] = List("Dont", "Do", "It")

val output: List[(String, Int)] = (testList.zipWithIndex)

My program is getting a bit of code-smell with using the ._1 & ._2 accessors. I created:

case class File(line: String, lineNum: Int)

However, the only way I know how to make best use of this case class would be to use the following:

val fileOutput: List[File] = for{(line, lineNum) <- output} yield{File(line, lineNum)}

My question: why can't I do this?

val output: List[File] = (testList.zipWithIndex)

I'm a bit weary of doing two passes on my file for the sake of using a case-class.

Thanks in advance


原文:https://stackoverflow.com/questions/40393617
更新时间:2022-06-25 09:06

最满意答案

使用FileStream的代码:

    byte[] buffer = new byte[1024];
    int numberOfBytesRead = 0;

    FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write);
    do
    {
        numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length); //Read from network stream
        fs.Write(buffer, 0, numberOfBytesRead);
    } while (serverStream.DataAvailable);
    fs.Close();

Code using FileStream :

    byte[] buffer = new byte[1024];
    int numberOfBytesRead = 0;

    FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write);
    do
    {
        numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length); //Read from network stream
        fs.Write(buffer, 0, numberOfBytesRead);
    } while (serverStream.DataAvailable);
    fs.Close();

相关问答

更多
  • 除非.net使用的不是winsock,否则根据winsock参考: 发送功能的成功完成并不表示数据已成功发送并已收到收件人。 此功能仅表示数据已成功发送。 如果传输系统中没有可用的缓冲空间来保存要传输的数据,则发送将被阻塞,除非套接字已被置于非阻塞模式。 在非阻塞的面向流的套接字上,写入的字节数可以在1和请求的长度之间,具体取决于客户端和服务器计算机上的缓冲区可用性。 假设write是在底层调用send,那么对winsock文档的严格解释就会表明没有保证数据在返回时将其传递到管道的另一端。 这里是我引用的w ...
  • NetworkStream没有文件概念,它只是流式字节缓冲区的一个类。 您需要做的是提出某种形式的协议来将文件名发送到客户端。 您可以通过首先传输文件名和文件名的长度,然后传输文件大小,然后传输实际的文件内容来完成此操作。 客户端可以读取文件名长度,读入文件名,读入文件大小然后读入文件数据。 NetworkStream has no concept of files it's just a class for streaming byte buffers. What you will have to do ...
  • 问题可能如下: bw.Write(new FileInfo(fileLocation).Length); ... var dataLength = br.ReadInt32(); Length属性实际上是long类型(8字节)。 但是您正在将值读取为Int32 (4个字节),而在流中保留其他4个字节。 The problem could be the following: bw.Write(new FileInfo(fileLocation).Length); ... var dataLength = b ...
  • 这些是包装流 。 您可以围绕任何现有流创建GzipStream ,以将压缩数据读取或写入内部流。 These are wrapper streams. You can create a GzipStream around any existing stream to read or write compressed data to the inner stream.
  • 它看起来像你的客户端代码没有完成阅读整个内容:你应该有一个循环,在检查内容之前读到最后: int remaining = d.Length; int pos = 0; while (remaining != 0) { int add = networkStream.Read(d, pos, remaining); pos += add; remaining -= add; } 目前,您的代码将尽可能多地读取网络流选择的数据,以便最初提供给您,并停止检索剩余的字节。 It looks ...
  • 使用FileStream的代码: byte[] buffer = new byte[1024]; int numberOfBytesRead = 0; FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write); do { numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length) ...
  • 在您的服务器代码中,您忽略了NetworkStream.Read的结果,这是读取的实际字节数。 这可能小于您实际请求的字节数。 您需要继续调用Read,直到您收到所需的所有字节为止。 您在1次或多次写入之间看到的差异与网络堆栈中的缓冲/定时有关。 我怀疑你在单次写作时很幸运地收到了图像 - 但情况并非总是如此! 如上所述,您需要处理Read的结果。 代码示例: void ReadBuffer(byte[] buffer, int offset, int count) { int num; i ...
  • MSDN描述应该更好地理解为 Write方法阻塞,直到请求的字节数写入本地网络缓冲区或抛出SocketException 即写入将在另一端成功接收整个文件之前返回。 这也意味着当您关闭应用程序时,可能会继续发送当前在网络缓冲区中的任何内容。 阻止主线程直到成功接收整个文件的唯一方法是可能使用异步套接字,一旦发送完成,等待接收端发送某种确认,您必须实现。 The MSDN description should perhaps be better read as The Write method blocks ...
  • 我不确定我是否看到了你的所有代码,但这里有一个例子,如果我要使用TcpSockets而不是WCF,我会怎么做。 服务器端 public void StartServer() { TcpListener listener = null; TcpClient client = null; NetworkStream nwStream = null; try { listener = new TcpListener(IPAddress.Any, 4444 ...
  • 你忽略了Read的返回值。 不要那样做。 Read不会等到它读取您请求的所有数据。 您应该循环阅读,直到您阅读了所需的一切: byte[] data = new byte[dataSize]; int index = 0; while (index < dataSize) { int bytesRead = ns.Read(data, index, dataSize - index); if (bytesRead <= 0) { // Or whatever exce ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。